[英]I'm trying to find out what it's called
Is there an easier way to achieve this in JavaScript?有没有更简单的方法可以在 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.在变量
test3
有对象,它可以显式创建或通过函数创建。 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:在 Javascript ES6 之后,您可以为这样的实体创建一个类:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.