62,074
社区成员
发帖
与我相关
我的任务
分享
Defining a Class - Object Constructors
A new JavaScript class is defined by creating a simple function. When a function is called with the new operator, the function serves as the constructor for that class. Internally, JavaScript creates an Object, and then calls the constructor function. Inside the constructor, the variable this is initialized to point to the just created Object. This code snippet defines a new class, Foo, and then creates a single object of that class.
function Foo()
{
this.x = 1;
this.y = 2;
}
obj = new Foo;