gpt4 book ai didi

javascript - Lodash - 使用 _.create() 的值(value)是什么

转载 作者:行者123 更新时间:2023-11-30 12:27:12 24 4
gpt4 key购买 nike

Lodash库,与其他更传统的方法相比,使用 _.create() 处理类和实例的值(value)是什么?

Docs for create

最佳答案

我不认为create()旨在取代现有的 JavaScript 继承/原型(prototype)机制。根据我的经验,将一种类型的集合映射到另一种类型很方便:

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 ]
// ]

关于javascript - Lodash - 使用 _.create() 的值(value)是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com