Dojo.mixin, Dojo.extend, Dojo.delegate Analysis

dojo.mixin

dojo.mixin function signature is: function(/*Object*/obj, /*Object…*/props){; where obj is the target object, props is the source object, and multiple can be passed in. This method is used to mix properties from other objects (including function properties) into the target object. Then return to the target object. If there is a property in the mixed source object and the target object at the same time, the source object property will overwrite the target object property, but the prototype of the target object will not be modified.

Below we Take a look at the source code in dojo:

dojo.mixin = function(/*Object*/obj, /*Object...*/props){ if(!obj){ obj = { };} //If the obj target object does not exist, give an empty object to prevent exceptions for(var i=1, l=arguments.length; i

dojo.extend

dojo.extend function signature is: function(/*Object*/ constructor, /* Object...*/ props); Accepts a constructor and multiple source objects. If the first parameter is not a constructor, to be precise, it does not contain a prototype attribute. This function is used to mix attributes from multiple source objects into the prototype attribute of a constructor, and the constructor will be returned. This function is implemented based on dojo.mixin, and you can see from the source code:

< /span>

dojo.extend = function(/*Object*/ constructor, /*Object...* / props){ // summary: // Adds all properties and methods of props to constructor's // prototype, making them available to all instances created with // constructor. for(var i=1, l=arguments.length; i< l; i++){ d._mixin(constructor.prototype, arguments[i]);} return constructor; // Object};

Because the target object of the mix-in is the prototype property of the constructor, the object created by the constructor (new) will contain all the properties in the source object.

From the above, mixin is suitable for one-time mixing, and extend is suitable for permanent混入(因为改为了prototype)

dojo.delegate

This function returns a new object after the call, and will The [[prototype]] attribute is mixed with the new attribute, the source code is as follows:

dojo.delegate = dojo ._delegate = (function(){ //Temporary constructor function TMP(){} //Returns an anonymous function, which is actually the dojo.delegate function return function(obj, props){ TMP.prototype = obj; var tmp = new TMP(); //Although TMP.prototype is reset to null, the tmp object has been created, and its [[prototype]] property has pointed to obj, //so through the tmp object you can access the obj object Property TMP.prototype = null; //If props exists, props will be mixed into the tmp object, so the tmp object has all the properties of the props object if(props){ d._mixin(tmp, props);} return tmp ; // Return the tmp object };})();//Execute immediately

From the above, dojo .delegate and dojo_delegat e is the same, pointing to the same function object. In js, every object created will have a [[prototype]] attribute, which is the prototype attribute of the constructor that created this object. In FireFox, the [[prototype]] attribute can be accessed through __proto__, please see below Example:

function Foo() {this.name = "Foo";}var foo = new Foo();var de = dojo.delegate(foo, {address: "China"});console.info(de); //Foo {address="China", name="Foo"}//Because in In the delegate function, TMP.prototype = obj;console.info(de.__proto__==foo); //true

PS:dojo version is dojo-release-1.6.3

Leave a Comment

Your email address will not be published.