简体   繁体   中英

I'm trying to find out what it's called

Is there an easier way to achieve this in JavaScript? For example without creating a function? And what is it called?

Thank you

 function testmulti(id,name) { this.id = id; this.name = name; } var test3 = new Array(); test3=new testmulti("4","toto2"); console.log(test3);

It's called an object. In variable test3 there is object and it can be created explicit or by function. In my example you can create only single object. In your example you have builder pattern, which allows you to create multiple, different objects.

 test3 = { id: '4', name: 'toto2' }; console.log(test3);

In Javascript ES6 onwards, you can create a class for an entity like this:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

And then, create an object of the class like below:

const square = new Rectangle(10, 10);

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