BenGost
BenGost

Reputation: 91

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);

Upvotes: 1

Views: 58

Answers (2)

Gautam
Gautam

Reputation: 81

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);

Upvotes: 0

JSowa
JSowa

Reputation: 10572

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);

Upvotes: 1

Related Questions