Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中使用mixin_Javascript_Oop_Ecmascript 6_Mixins_Composition - Fatal编程技术网

如何在Javascript中使用mixin

如何在Javascript中使用mixin,javascript,oop,ecmascript-6,mixins,composition,Javascript,Oop,Ecmascript 6,Mixins,Composition,大家好,我有抽象类计算机: 类计算机{ 构造函数(制造商、处理器速度、ram、硬盘空间){ if(new.target==计算机){ 抛出新错误(“无法直接实例化”); } 这个。制造商=制造商; this.processorSpeed=编号(processorSpeed); this.ram=编号(ram); this.hardDiskSpace=编号(hardDiskSpace); } }这些评论提出了一个很好的问题,即您是否真的想在这里使用mixin。但是如果你这样做了,你可能想看看和的

大家好,我有抽象类计算机:

类计算机{
构造函数(制造商、处理器速度、ram、硬盘空间){
if(new.target==计算机){
抛出新错误(“无法直接实例化”);
}
这个。制造商=制造商;
this.processorSpeed=编号(processorSpeed);
this.ram=编号(ram);
this.hardDiskSpace=编号(hardDiskSpace);
}

}
这些评论提出了一个很好的问题,即您是否真的想在这里使用mixin。但是如果你这样做了,你可能想看看和的文章

使用前一种技术,您可以将

const asComputerQuality = function() {
  this.getQuality = function() {
    return this.processorSpeed
           * this.ram
           * this.hardDiskSpace;
  };
  this.isFast = function() {
    return this.processorSpeed > this.ram / 4;
  };
  this.isRoomy = function() {
    return this.hardDiskSpace > Math.floor(this.ram * this.processorSpeed);
  };
}

asComputerQuality.call(Computer.prototype);

然后您应该能够在您的
计算机上调用这些方法。

mixin应该被视为一种方便的代码重用形式

描述对象的某些行为以及 倾向于一次又一次地被复制,可能会被考虑 一次收集/储存到混合器中

使用JavaScript中基于函数的mixin/trait模式 您还可以使用它的有状态变体,它提供 甚至有更多的可能性,人们可能会如何安排 一种类型/对象体系结构

正如已经指出的那样,OP的例子并不那么好 选择适合混合/基于特征的成分

不过,下一个给定的代码块确实尝试使用 更改了变体,以演示不同的方法 在JavaScript中应用基于函数的mixin/trait模式的过程

带有ObjectBaseIntrospection(state){//-mixin的函数,该函数保留注入的
var//通过创建
object=this;//调用/应用时的闭包。
object.valueOf=函数(){
返回Object.assign({},state);
};
object.toString=函数(){
返回JSON.stringify(state);
};
}
具有硬件标准获取程序(状态){//-mixin的函数,该函数保留注入的
var//通过创建
hardware=this;//在调用/应用时关闭。
Object.defineProperty(硬件,“制造商”{
get:function(){return state.manufacturer;}
});
定义属性(硬件,“处理器速度”{
get:function(){return state.processorSpeed;}
});
Object.defineProperty(硬件,“ram”{
get:function(){return state.ram;}
});
Object.defineProperty(硬件,“硬盘空间”{
get:function(){return state.hardDiskSpace;}
});
}
使用desktopspecificgetters(state){/-mixin保留注入的
var//通过创建
hardware=this;//在调用/应用时关闭。
Object.defineProperty(硬件,“bodyLength”{
get:function(){return state.bodyLength;}
});
Object.defineProperty(硬件,“bodyWidth”{
get:function(){return state.bodyWidth;}
});
Object.defineProperty(硬件,“车身高度”{
get:function(){return state.bodyHeight;}
});
}
具有HardwarSpecificQuality()的函数{//-基于通用函数的混合模式。
this.getQuality=函数(){
返回(this.processorSpeed*this.ram*this.hardDiskSpace);
};
this.isFast=函数(){
返回(this.processorSpeed>(this.ram/4));
};
this.isRoomy=函数(){
返回(this.hardDiskSpace>Math.floor(this.ram*this.processorSpeed));
};
}
具有DeskTopSpecificMeasures()的函数{//-基于通用函数的混合模式。
this.getBodyVolume=函数(){
返回值(this.bodyLength*this.bodyWidth*this.bodyHeight);
};
}
班级电脑{
建造商(州){
withObjectBaseIntrospection.call(this,state);//-应用2“有状态混合”
withHardwareStandardGetters.call(this,state);//在实例/对象级别。
}
}
使用硬件特定质量。调用(计算机。原型);//-通过
//构造函数的原型,但丰富了
//后者通过更通用的mixin(在“类级别”)。
类桌面扩展计算机{//-新提供
构造函数(state){//语法糖更多
//“类”继承模式。
超级(州);
withDesktopSpecificGetters.call(this,state);//-应用“有状态混合”
}//在实例/对象级别。
}
withDesktopSpecificMeasures.call(Desktop.prototype);//-通过
//构造函数的原型,但丰富了
//后者通过更通用的mixin(在“类级别”)。
让
桌面=新桌面({
制造商:“JAR计算机”,
处理器速度:3.3,
ram:8,
硬盘空间:1,
车身宽度:300,
身高:40,
体长:300
});
log(“Desktop.prototype:”,Desktop.prototype);
console.log(“Computer.prototype:”,Computer.prototype);
log(“(桌面的桌面实例”),(桌面的桌面实例));
console.log(“(计算机的桌面实例”),(计算机的桌面实例));
console.log(“desktop.manufacturer:”,desktop.manufacturer);
log(“desktop.processorSpeed:”,desktop.processorSpeed);
log(“desktop.ram:”,desktop.ram);
console.log(“desktop.hardDiskSpace:”,desktop.hardDiskSpace);
log(“desktop.getQuality():”,desktop.getQuality());
log(“desktop.getBodyVolume():”,desktop.getBodyVolume());
清汤