关于javascript:如何在Quasar中添加自定义属性?

How to add custom property in Quasar?

我想在Quasar框架中添加自定义属性,但是当我设置它时,ESlint向我显示此错误:
Array prototype is read only, properties should not be added

我想为数组添加一个扩展方法:

1
2
3
4
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

1
2
3
4
5
let extend = function(other_array) {
  return other_array.forEach(function(v) {this.push(v)}, this)
}

export default extend;

componentA.vue

1
2
3
import extend from './helpers.js';

// use extend as a normal function

或者我们可以变得更聪明一些,并使用Javascript已经拥有的本机方法:

1
2
3
4
5
// will 'glue' two arrays together
firstArray.concat(secondArray);

// or using new ECMA syntax (spread operator)
finalArray = [...firstArray, ...secondArray];