Four methods of JS function invocation: Method invocation mode, function call pattern, constructor invocation pattern, Apply,call invocation pattern
1. Method invocation Pattern:
The current MyObject object is defined by defining an object and then defining the method in the object's properties by Myobject.property to execute the method.
var bloginfo={
blogid:123,
blogname: "WERWR",
showblog:function () {alert (this.blogid);}
;
Bloginfo.showblog ();
2. Function call mode
Define a function, set a variable name save function, then this point to the Window object.
var myfunc = function (a,b) {return
a+b;
}
Alert (MyFunc (3,4));
3. Constructor call pattern
Defines a function object, defines a property in an object, and defines a method in its prototype object. When you use the prototype method, you must instantiate the object to invoke its methods.
var myfunc = function (a) {
this.a = A;
};
Myfunc.prototype = {
show:function () {alert (THIS.A);}
}
var newfunc = new MyFunc ("123123123");
Newfunc.show ();
4. Apply,call Call mode
var myobject={};
var sum = function (a,b) {return
a+b;
};
var sum2 = Sum.call (myobject,10,30); var sum2 = sum.apply (myobject,[10,30]);
alert (SUM2);
The above definition of JS function and call method recommendation is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.