A function and an object are as follows:
Copy CodeThe code is as follows:
var doubling=function (x) {
return x*2;
};
var obj={
VAL:100,
};
When the function calls the pattern, this is bound to the global object. This behavior can also be reflected when the object's properties and methods are initialized. The OJB is now supplemented as follows:
Copy CodeThe code is as follows:
var obj={val:100,
Prop:function () {
var that=this;
document.write (' Name: ' +that+ '; Type: ' +typeof (that) + '
');
Return doublling (That.val);
}(),
Get_prop:function () {
var that=this;
document.write (' Name: ' +that+ '; Type: ' +typeof (that) + '
');
Return doublling (That.val);
},
};
Prop uses an anonymous function that is executed, expecting to get the result of the doubling () operation of the Val value of the object being called by the function call pattern, and Get_prop as the method invocation pattern.
When the script loads, the property of obj is prop initialized with the statement "name: [Object Window]; Type:object "output, when using Obj.get_prop (), the statement" Name: [Object Object]; Type:object "output. The former indicates that the "this" of the function body is a global variable window, which is expected to be obj itself.
You can check the return value of the property prop and Method Get_prop (), which multiplies the Window object and returns Nan, which equals 200.
Outside of the obj literal expression, the New_prop property and the New_get_prop () method are expected to be set, and the result will be consistent with the previous text, and the method invocation pattern will obtain the binding of this to itself.
http://www.bkjia.com/PHPjc/324210.html www.bkjia.com true http://www.bkjia.com/PHPjc/324210.html techarticle existing functions and objects are as follows: The copy code code is as follows: Var doubling=function (x) {return x*2;}; var obj={val:100,}; When the function calls the pattern, this is bound to the global object. This kind of ...