Php method call pattern and function call Pattern Simple example _php tutorial

Source: Internet
Author: User
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 ...

  • 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.