猿问

如何在 Quasar 中添加自定义属性?

我想在 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)

}


白衣非少年
浏览 160回答 1
1回答

呼如林

当你扩展一个对象时,你改变了它的行为。更改仅由您自己的代码使用的对象的行为很好。但是,当您更改其他代码也使用的某些内容的行为时,您可能会破坏其他代码。您可以在这里选择创建一个函数并将其导入:helpers.jslet extend = function(other_array) {  return other_array.forEach(function(v) {this.push(v)}, this)}export default extend;组件A.vueimport extend from './helpers.js';// use extend as a normal function或者我们可以更聪明一点,使用 Javascript 已经拥有的本地方法:// will 'glue' two arrays togetherfirstArray.concat(secondArray);// or using new ECMA syntax (spread operator)finalArray = [...firstArray, ...secondArray];
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答