白衣非少年
2022-05-26 17:47:19
我想在 Quasar 框架中添加自定義屬性,但是當(dāng)我設(shè)置它時,ESlint 向我顯示了這個錯誤: Array prototype is read only, properties should not be added我想為數(shù)組添加一個擴展方法: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條經(jīng)驗 獲得超3個贊
當(dāng)你擴展一個對象時,你改變了它的行為。
更改僅由您自己的代碼使用的對象的行為很好。但是,當(dāng)您更改其他代碼也使用的某些內(nèi)容的行為時,您可能會破壞其他代碼。
您可以在這里選擇創(chuàng)建一個函數(shù)并將其導(dǎo)入:
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 已經(jīng)擁有的本地方法:
// will 'glue' two arrays together
firstArray.concat(secondArray);
// or using new ECMA syntax (spread operator)
finalArray = [...firstArray, ...secondArray];
添加回答
舉報
0/150
提交
取消