Javascript Lodash-使用ux.create()的价值是什么
在库中,与其他更传统的方法相比,使用Javascript Lodash-使用ux.create()的价值是什么,javascript,lodash,Javascript,Lodash,在库中,与其他更传统的方法相比,使用.create()处理类和实例有什么价值 我认为这并不意味着要取代现有的JavaScript继承/原型机制。根据我的经验,将一种类型的集合映射到另一种类型时非常方便: function Circle(x, y) { this.x = x; this.y = y; } function Square(x, y) { this.x = x; this.y = y; } Square.prototype.coords = func
.create()
处理类和实例有什么价值
我认为这并不意味着要取代现有的JavaScript继承/原型机制。根据我的经验,将一种类型的集合映射到另一种类型时非常方便:
function Circle(x, y) {
this.x = x;
this.y = y;
}
function Square(x, y) {
this.x = x;
this.y = y;
}
Square.prototype.coords = function() {
return [ this.x, this.y ];
}
var collection = [
new Circle(1, 1),
new Circle(2, 2),
new Circle(3, 3),
new Circle(4, 4)
];
_(collection)
.map(_.ary(_.partial(_.create, Square.prototype), 1))
.invoke('coords')
.value();
// →
// [
// [ 1, 1 ],
// [ 2, 2 ],
// [ 3, 3 ],
// [ 4, 4 ]
// ]
我认为这是一种方便。在JS中实现经典继承模型这一常见任务时,它会更简洁一些 本机:
var User = function() {};
User.prototype = Object.create(Person.prototype);
Object.assign(User.prototype, {
constructor: User,
...other stuff
});
与相比。创建:
var User = function() {};
User.prototype = _.create(Person.prototype, {
constructor: User,
...other stuff
});
在阅读了一些lodash代码后,我看到的最大区别是Object.create第二个参数采用Object.defineProperty arg的格式,它是一个属性描述符,而u.create只复制所有自己的或继承的可枚举属性(依赖于nativeKeysIn)从这个对象
它主要简化了经典对象定义。看起来像是对象的填隙片。create
@DanielA.White:但不完全是这样,因为第二个参数不包含属性描述符。可能重复(和类似问题)。请你的问题详细说明你所说的“更传统的方法”是什么意思,否则我们需要结束它。当你说“使用u.create()的价值是什么”时,你的意思是说完全不使用低破折号,以不同的方式创建对象,或者你的意思是,与仍然使用lowdish而创建没有uu.create的对象相反?我的意思是,使用.create()
处理继承与任何其他处理继承的方法(如标准的Class.prototype.method\u name=function()…