javascript的Mixins
来源:互联网 发布:mac梦幻西游更新失败 编辑:程序博客网 时间:2024/05/22 02:01
mixin在javascript里可以看作是一种从别的对象”借用”功能的方法。每一个新定义的对象都有一个 prototype属性,其他的对象就可以从这里”借用”功能。这里的功能可以是一个属性,也可以是一个方法。
mixins这种借用在 javascript里非常的适用。在重用代码的时候可以使用mixins来实现继承,也可以达到类似多继承的效果。假设我们定义了这么一个对象:
var myMixins = { moveUp: function(){ console.log( "move up" ); }, moveDown: function(){ console.log( "move down" ); }, stop: function(){ console.log( "stop! in the name of love!" ); }};
我们可以非常容易的使用一个helper来扩展现有的对象。比如使用Underscore.js
的extend()
方法:
// A skeleton carAnimator constructorfunction carAnimator(){ this.moveLeft = function(){ console.log( "move left" ); };}// A skeleton personAnimator constructorfunction personAnimator(){ this.moveRandomly = function(){ /*..*/ };}// Extend both constructors with our Mixin_.extend( carAnimator.prototype, myMixins );_.extend( personAnimator.prototype, myMixins );// Create a new instance of carAnimatorvar myAnimator = new carAnimator();myAnimator.moveLeft();myAnimator.moveDown();myAnimator.stop();// Outputs:// move left// move down// stop! in the name of love!
从代码可以看到,这个mixins实现的非常简单。在下一个例子中我们会使用两个构造函数:一个Car
,一个Mixin
。我们要做的就是使用一个自定义的argument方法来扩展Car,这样Car
可以从Mixin
里”借用”某些特定的方法。比如,driveForward()
和driveBackword()
。这次我们不使用Underscore.js
。
这里例子会非常清楚的展示argument方法是怎么达到”借用”效果的:
// Define a simple Car constructorvar Car = function ( settings ) { this.model = settings.model || "no model provided"; this.color = settings.color || "no colour provided"; };// Mixinvar Mixin = function () {};Mixin.prototype = { driveForward: function () { console.log( "drive forward" ); }, driveBackward: function () { console.log( "drive backward" ); }, driveSideways: function () { console.log( "drive sideways" ); }};// Extend an existing object with a method from anotherfunction augment( receivingClass, givingClass ) { // only provide certain methods if ( arguments[2] ) { for ( var i = 2, len = arguments.length; i < len; i++ ) { receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]]; } } // provide all methods else { for ( var methodName in givingClass.prototype ) { // check to make sure the receiving class doesn't // have a method of the same name as the one currently // being processed if ( !Object.hasOwnProperty(receivingClass.prototype, methodName) ) { receivingClass.prototype[methodName] = givingClass.prototype[methodName]; } // Alternatively: // if ( !receivingClass.prototype[methodName] ) { // receivingClass.prototype[methodName] = givingClass.prototype[methodName]; // } } }}// Augment the Car constructor to include "driveForward" and "driveBackward"augment( Car, Mixin, "driveForward", "driveBackward" );// Create a new Carvar myCar = new Car({ model: "Ford Escort", color: "blue"});// Test to make sure we now have access to the methodsmyCar.driveForward();myCar.driveBackward();// Outputs:// drive forward// drive backward// We can also augment Car to include all functions from our mixin// by not explicitly listing a selection of themaugment( Car, Mixin );var mySportsCar = new Car({ model: "Porsche", color: "red"});mySportsCar.driveSideways();// Outputs:// drive sideways
好处和坏处
Mixins可以减少代码的重复增加代码的复用。如果一个对象需要使用其他对象已经定义的”功能”的时候,我们就可以使用mixins复用代码。这样就可以集中精力实现那么独一无二,确实非常需要的代码上。
但是,mixins也有值得商榷的一面。有很多开发者认为把方法注入到其他的对象里不是很好,这样会造成prototype污染,也会造成我们本来定义的对象的不确定性。这些确实会发生。
我个人觉得良好的文档会减少mixins的使用造成的困惑。而且,不管任何的模式。只要我们在开发的时候就考虑好它的利和弊,那么就会减少不必要的问题。
原文地址:https://www.safaribooksonline.com/library/view/learning-javascript-design/9781449334840/ch09s13.html
- javascript的Mixins
- vue的mixins属性
- 十个有用的Sass Mixins
- 几个实用的Sass mixins
- Scss强大的Mixins指令
- 关于extjs5.0下的mixins
- vue mixins和extends的妙用
- bootsrap的mixins编译错误已经在github上修复
- SASS基础——十个常见的Mixins
- Django基于类的编辑视图和Mixins
- [Dart] Dart中mixins(混入)功能的简单解释
- Reactjs Mixins
- Reactjs Mixins
- mixins详解
- Reactjs Mixins
- vue -- mixins
- mixins混合
- Ext4 组合 mixins
- 剑指offer题解 合并两个排序的链表
- 解决linux命令行使用不了的情况
- matlab中CRC的函数使用
- Hanoi Tower 汉诺塔的简单分析/C
- 【每天一道剑指offer】2:矩阵中的路径
- javascript的Mixins
- 关于启动MongDB的mongod.exe文件闪退的问题
- 在viewController中隐藏statusBar
- 磁盘管理
- Building Good Docker Images
- iOS中 为 iOS 建立 Travis CI 韩俊强的博客
- Linux压缩与解压缩
- 用windbg寻找设备树根节点
- apk动态加载机制(二) : 资源加载和activity生命周期管理