【发布时间】:2017-12-21 14:41:38
【问题描述】:
我对原型有疑问。 我正在做一个项目,我想为 Array 创建一个小型函数库,我有 3 种可能性。
扩展Array原型(但我明白不推荐)
Array.prototype.arrayMax = function () {
return Math.max(...this)
}
创建 Array 的子类。
function MyArray(...args) {
Object.setPrototypeOf(args,MyArray.prototype)
return args;
}
MyArray.prototype = Object.create(Array.prototype);
MyArray.prototype.arrayMax = function () {
return Math.max(...this)
}
创建一个包含不同函数的文件,其中 Array 通过参数。
const arrayMin = arr => Math.min(...arr);
在哪里可以添加我这个归档 js。
什么是最正确的选择?
【问题讨论】:
标签: javascript prototype prototypal-inheritance prototype-programming