我想在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)
}
扩展对象时,将更改其行为。
更改仅由您自己的代码使用的对象的行为就可以了。但是,当您更改其他代码也使用的某些行为时,就有可能破坏该其他代码。
您可以在此处选择创建一个函数并将其导入:
helpers.js
let extend = function(other_array) {
return other_array.forEach(function(v) {this.push(v)}, this)
}
export default extend;
componentA.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];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句