javascript'this'
2198 ワード
1.Scope of this in object:
2.this in Htmlelemens:
http://www.quirksmode.org/js/this.html
3.A more detailed introduction can be found here.
http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/
var b = {a:outerFunc, b:"b"}
function outerFunc(base) {
var outer=this;
var punc="!";
function returnString(ext) {
//console.log(this===outer);
console.log(outer);
console.log(this);
return base + ext + punc;
}
return returnString;
}
var baseString = b.a("Hello ");// method 'a' is called from the scope of object 'b', 'this' refer to the object of 'b'
, so outer will be assigned object 'b'
baseString("World"); //baseString is called from the window scope, the 'this' in the mothod of 'returnString' now
refer to the window object
var o={}; // declaring new object
o.name="moon";
o.method=function () {
alert(this.name);
};
var x={};
x.name="sun";
x.method=o.method;
x.method();//Alert sun here
var o={}; // declaring new object
o.name="moon";
o.method=function () {
alert(this.name);
};
var x={};
x.name="sun";
o.method.call(x);//alert "sun here"
// For more, refer to http://devign.me/javascript-this-keyword/
2.this in Htmlelemens:
http://www.quirksmode.org/js/this.html
3.A more detailed introduction can be found here.
http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/