簡體   English   中英

JavaScript編輯對象內部的數組

[英]JavaScript edit Array inside Object

是否可以將項目推送到JavaScript對象內部的數組

這是我的代碼:

function test() {
this.array = [];

this.addItem = function() {
    this.array.push("someString");
}

this.removeItem = function() {
    this.array.remove(0);
}}

看這段代碼-

 var obj = { numbers: [1, 2, 3, 4] } obj.numbers.push(5) console.log(obj.numbers) 

如果要使用對象,則可以使用Javascript ES6

 class Test { constructor() { this.array = []; } addItem(item) { this.array.push(item); } removeItem() { this.array.splice(0, 1); } removeItemByIndex(index) { this.array.splice(index, 1); } } const test = new Test(); test.addItem('Some Item'); test.addItem('Some Item 2'); console.log(test.array); test.removeItem(); console.log(test.array); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM