Simple example of PHP method invocation pattern and function call pattern

Source: Internet
Author: User

An existing function and object are as follows:

When the
var doubling=function (x) {
return x*2;
};
var obj={
VAL:100,
};
function calls the pattern, this is bound to the global object. This situation can also be reacted when the object's properties and methods are initialized. The OJB is now supplemented as follows:


var obj={val:100,
Prop:function () {
var that=this;
document.write (' Name: ' +that+ '; Type: ' +typeof ' + ' <br/> ');
Return doublling (That.val);
}(),
Get_prop:function () {
var that=this;
document.write (' Name: ' +that+ '; Type: ' +typeof ' + ' <br/> ');
Return doublling (That.val);
},
};
prop uses an anonymous function that is executed, expecting that the Val value of the object be the result of the doubling () operation of the function call pattern, and Get_prop for the method invocation pattern.


When the script is loaded, Obj's property 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 the global variable window, which is expected to be the obj itself.


can check the return value of the property prop and Method Get_prop (), which multiplies the Window object and returns Nan, which equals 200.


the New_prop property and the New_get_prop () method are expected to be set outside of the obj literal expression, the result will be consistent with the previous text, and the method invocation pattern will get the binding of this to itself.


Detailed Source reference: Http://www.jb51.net/article/28345.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.