简体   繁体   中英

How to declare an array as a property of an object in an object constructor?

I'm trying to add an array as a property to an object in its constructor, like so:

let object {
  entries = [],
}

But when I try to access the "entries" property, either inside a function of the object itself, or in an outside function, it isn't recognized as an array.

I think you just have a syntax error in constructing the object, there shouldn't be any issue accessing an array in an object:

 let object = { entries: [], getEntries() { return this.entries; } } object.entries.push('it works'); console.log(object.entries) console.log(object.getEntries())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM