简体   繁体   中英

How to add an array property in an object constructor?

const myCountry = function (country, capital, language, population) {

     (this.country = country),

     (this.capital = capital),

     (this.language = language),

     (this.population = population),
 
     (this.neighbours = []);
   
};

const Utopia = new myCountry("Utopia", "rabat", "arabic", "18 million", [
  "algeria",
  "tunisia",
]);

use spread syntax

 class myCountry { constructor(country, capital, language, population, neighbours) { this.country = country this.capital = capital this.language = language this.population = population this.neighbours = [...neighbours] } } const Utopia = new myCountry('Utopia', 'rabat', 'arabic', '18 million', ['algeria','tunisia']) console.log( Utopia.neighbours )

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