1. Method Invocation Mode:
var myObj = { value:0; Increment:function (inc) { This.value + = typeof Inc = = = ' number '? Inc:1; }} Myobj.increment (); Console.info (Myobj.value); 1
2. Function call Mode:
When a function is not a property of an object, then it is used as a function, and this is bound to the global object, in order to solve this problem, we need to rename a variable internally. Get control of an intrinsic function
myobj.double = function () { var _self = this; var helper = function () { _self.value = Add (_self.value,_self.value); } Helper (); Function call Helper};myobj.double (); Console.info (Myobj.value);
3. Constructor Mode:
If you call with new in front of a function, you will create a new object that hides the prototype that is connected to the function, and this will be bound to the new object.
var Quo = function (str) { this.status = str;} Quo.prototype.get_status = function () { return this.status;} Constructs a Quo instance var myquo = new Quo ("abc"); Console.info (Myquo.get_status ());
4. Apply Mode:
The Apply method constructs a parameter array and uses it to invoke the function, and allows us to select the this value, apply receives two parameters, one is the value that will be bound to this, and the other is the parameter array.
var arr = [3,4];var sum = add.apply (Null,arr);//Constructs an object containing the status member var statusobj = { Status: "OK"};//statusobj does not inherit from Qu O.prototype, but the Get_status method can be raised in Statusobj, although Statusobj does not have a method named Get_status. var status = Quo.prototype.get_status.apply (Statusobj); Console.info (status); "OK"
JavaScript function call pattern