var Something = { cool: function () { this.greeting = 'Hello World'; this.count = this.count ? this.count + 1 : 1; } } Something.cool(); Something.greeting; // 'Hello World' Something.count; // 1 var Another = { cool: function() { // 隐式把 Something 混入 Another Something.cool.call(this); } }; Another.cool(); Another.greeting; // 'Hello World' Another.count; // 1
By using Something.cool.call (this), or call the constructor method call; actually "borrowed" function Something.cool () and in the context Another call it. The end result is Something.cool () the assignment will apply to Another object. Therefore, we Something act "mixed" to Another in.
Although this type of technology takes advantage of this function of re-binding, master Something.cool.call (this) but still can not become a relatively more flexible references, so be careful when using. In general, to avoid such a configuration, in order to ensure cleanliness and maintainability of the code.