JavaScript uses the function apply to extend the Environment object
The Apply method of invoking the constructor object is passed as a parameter through the instance object.
So that the instance object is executed as an environment object in the constructor as a normal function,
The properties of the constructor are overridden on the instance object to implement the property extension of the instance object.
1. Apply and call incoming parameters for function objects
var tag = "Global"; function say () { for (var args = "", i = 0; i < arguments.length; ++i) args += arguments[i] + ","; console.log (this.tag + ": " + args); } var obj = { tag: "obj", sayobj: function ( ) { console.log (This.tag); } };   /**apply accepts environment objects and arrays, call accepts environment objects and scattered array parameters */ say.apply (obj, [1, 3, &NBSP;7,&NBSP;11]);//obj: 1,3,7,11, say.call (obj, 2, 4, [8, 14 ]);//obj: 2,4,8,14, say ();//global /** The This in the inner of the Obj object is also a pointer to global, and this of the function inside the object points to this object */ //obj.say (); //uncaught Typeerror: obj.say is not a function obj.sayobj (); //obj
2. Only the function object has the Apply method call
var x = {Name: "x", Sayx:function () {Console.log ("x:" + this.name); } }; var y = {name: "Y"}; /** calls the Sayx of the X object to print out x:x*/x.sayx (); X:x/** Normal object x does not have an apply method for calling *///x.apply (y); Error:x.apply is not a function/** functions X.sayx executed in the environment of Y object, print out x:y*/x.sayx.apply (y); X:y/** in Y extends the properties under the Sayx function, not the SAYX itself *///y.sayx (); Error:y.sayx is not a function
3. Use apply to extend its object properties on a Function object
function x () { this.name = "x"; this.age = 11; this.say = function () { console.log (this.name); } } function y () { this.name = "Y"; this.color = "BLACK"; } /** calls the constructor X to return an instance x*/ var x = New x (); x.say (); //x console.log (X); //x {name: "x", age: 11, say: function ()} /** extends objects with properties in function object x Y*/ var y = new y (); console.log (y); //y {name: "y", color: "Black"} x.apply (y); console.log (y ); //y {name: "x", color: "Black", age: 11, say: function ()} The /** function X performs the assignment of the name in the object environment Y to the value in X X*/ y.say (); //x y.name = "Y"; y.say (); //y
4. The reason is that the Environment object executes the code in the function as this
var name = "global"; var obj = { name: "obj", callname: function () {this.foo = function () { console.log ("Deceived IDE"); }; console.log (this.name); } }; obj.callname (); //obj /** when the parameter is not passed to apply, the default global environment as the first parameter At the same time, the global also obtains the attributes in the original environment */ obj.callname.apply (); /** run the function in the original obj environment, The value of the search to the nearest value or obj property name Obj*/ obj.callname (); //obj /** * when not performing the above aPply, it will be an error, of course, it is not recommended to do so * Uncaught ReferenceError: foo is not defined * runs the this.foo= when you execute the Apply ..., where this is the global environment at runtime * So when you execute foo below, it prints out */ foo ();//deceived the IDE
5. Function expressions mimic block-level scopes to avoid global variable collisions
/** * converts a function declaration into a function expression through parentheses and executes it immediately * the environment inside this function is the environment of this function, thus avoiding the global variable Conflict */(function () {Var person = { name: "Ameki", age: 18, sayhi: function () { alert ("hello, " + this.name); } };for (Var prop in person) { console.log (prop + ", " + person[prop]); } }) (); /* name, ameki age, 18 sayHi, function () { alert ("hello, " + this.name); } */
If you think this article will help you, please click the recommended button at the bottom right, so that more small partners can see it.
This address:http://www.cnblogs.com/kodoyang/p/Javascript_FunctionApply_ExtendEnvironmentObject.html
Rain Yoko
September 22, 2015
JavaScript uses the function apply to extend the Environment object