我想在 Quasar 框架中添加自定义属性,但是当我设置它时,ESlint 向我显示了这个错误: Array prototype is read only, properties should not be added我想为数组添加一个扩展方法:Array.prototype.extend = function (other_array) { /* You should include a test to check whether other_array really is an array */ other_array.forEach(function(v) {this.push(v)}, this)}
1 回答

呼如林
TA贡献1798条经验 获得超3个赞
当你扩展一个对象时,你改变了它的行为。
更改仅由您自己的代码使用的对象的行为很好。但是,当您更改其他代码也使用的某些内容的行为时,您可能会破坏其他代码。
您可以在这里选择创建一个函数并将其导入:
helpers.js
let extend = function(other_array) {
return other_array.forEach(function(v) {this.push(v)}, this)
}
export default extend;
组件A.vue
import extend from './helpers.js';
// use extend as a normal function
或者我们可以更聪明一点,使用 Javascript 已经拥有的本地方法:
// will 'glue' two arrays together
firstArray.concat(secondArray);
// or using new ECMA syntax (spread operator)
finalArray = [...firstArray, ...secondArray];
添加回答
举报
0/150
提交
取消