在 JavaScript 中实例化新对象

发布于 2024-12-22 04:28:12 字数 575 浏览 2 评论 0原文

我可能缺少一些关于 javascript 的基本知识,但是为什么这不起作用呢?

var person = {

    name:"",
    age:0,

    set : function(name,age){
        this.name = name;
        this.age = age;
    }
}

var me = new person; // ERROR: Object doesn't support this action!
me.set('Victor',12);
me.name //Victor

但这确实:

var person = function(){

    this.name="";
    this.age=0;

    this.set = function(name,age){
        this.name=name;
        this.age=age;
    }
}

var me = new person(); //WORKS!
me.set('Victor',12);
me.name; //Victor

I'm probably missing something really basic on javascript knowledge, but why doesn't this work?

var person = {

    name:"",
    age:0,

    set : function(name,age){
        this.name = name;
        this.age = age;
    }
}

var me = new person; // ERROR: Object doesn't support this action!
me.set('Victor',12);
me.name //Victor

But this does:

var person = function(){

    this.name="";
    this.age=0;

    this.set = function(name,age){
        this.name=name;
        this.age=age;
    }
}

var me = new person(); //WORKS!
me.set('Victor',12);
me.name; //Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若无相欠,怎会相见 2024-12-29 04:28:12

您的第一个示例是对象文字:

var person = {
    name: "Bob",
    //etc
};

您创建了一个名为 person 的对象,仅此而已。创造新人的概念没有任何意义。

如果您想创建可用于随意创建更多对象的东西,您可以使用一个函数,就像第二个示例一样。

但请注意,按照惯例,打算用作构造函数的函数以大写字母开头。另请注意,当您在构造函数内部设置函数时,使用

this.functionName = function() .....

此函数会创建一个特权函数,因为它可以访问公共和私有成员。如果此设置函数仅需要访问公共属性,则通常会将其添加到函数的原型中。整个事情可能是这样的

function Person() {
    this.name = "";
    this.age = 0;
};

Person.prototype.set = function(name,age) {
    this.name = name;
    this.age = age;
}

特权方法可能是这样的

function Person() {
    var localData = "Hello";

    this.showPrivateData = function() {
        alert(localData);
    };

    this.name="";
    this.age=0;
};

localDataPerson 函数的本地方法,并且不能作为 Person 实例上的属性进行访问;但是,showPrivateData 函数以及您可能添加的任何其他特权函数将在其上形成一个闭包,并可以访问它。

最后,请注意构造函数可以带参数:

function Person(name, age) {
    this.name= name;
    this.age= age;
};

Your first example was an object literal:

var person = {
    name: "Bob",
    //etc
};

You've created a single object called person, and that's that. The concept of creating new persons has no meaning.

If you want to create something that can be used to create more objects at will, you'd use a function, like your second example.

Note though that functions intended to be used as a constructor start with a capital letter by convention. Also note that when you set a function inside of your constructor using

this.functionName = function() .....

this creates a privileged function, since it has access to both public, and private members. If this set function only needs access to public properties, it would usually be added to the function's prototype. Here's how the whole thing might look

function Person() {
    this.name = "";
    this.age = 0;
};

Person.prototype.set = function(name,age) {
    this.name = name;
    this.age = age;
}

And here's what a privileged method might look like

function Person() {
    var localData = "Hello";

    this.showPrivateData = function() {
        alert(localData);
    };

    this.name="";
    this.age=0;
};

localData is local to the Person function, and cannot be accessed as a property on instances of Person; however, the showPrivateData function, and any other privileged functions you might add, would form a closure over it, and have access to it.

Finally, note that constructor functions can take parameters:

function Person(name, age) {
    this.name= name;
    this.age= age;
};
懵少女 2024-12-29 04:28:12

你回答你自己的问题。构造函数(new 后面的内容)必须是 JavaScript 中的函数。

You answer your own question. A constructor (the thing that goes next to new) must be a function in JavaScript.

傲性难收 2024-12-29 04:28:12

JavaScript 没有类(尽管您可以模拟它们),这就是原因。并且 new 仅适用于函数(在本例中,它在函数内部创建特殊变量 this,该变量在函数调用后隐式返回)。

因此,当使用它时:

var person = {
    name:"",
    age:0,
    set : function(name,age){
        this.name = name;
        this.age = age;
    }
};

您已经创建了一个对象(该对象具有一个函数作为其属性之一)。

但是,当您使用 this 时:

var person = function(){
    this.name="";
    this.age=0;
    this.set = function(name,age){
        this.name=name;
        this.age=age;
    }
};

您仅创建函数,当使用 new 调用该函数时,该函数会返回 this 以及所有分配的属性。

文档内容

作为 Mozilla 上的 new 运算符的文档开发者网络 说:

new 运算符创建用户定义的对象类型或具有构造函数的内置对象类型之一的实例。

...

创建用户定义的对象需要两个步骤:

  • 通过编写函数定义对象类型
  • 使用new创建对象的实例

在第二种情况下,您基本上执行了这两个步骤。

JavaScript has no classes (although you can simulate them), this is why. And new works only on functions (in this case it creates special variable this inside function, which is being returned implicitly after function call).

So, when using this:

var person = {
    name:"",
    age:0,
    set : function(name,age){
        this.name = name;
        this.age = age;
    }
};

you already create an object (which has a function as one of its properties).

But when you use this:

var person = function(){
    this.name="";
    this.age=0;
    this.set = function(name,age){
        this.name=name;
        this.age=age;
    }
};

you only create function, that returns this with all assigned properties when the function is called with new.

What documentation says

As documentation of new operator on Mozilla Developer Network says:

The new operator creates an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

...

Creating a user-defined object requires two steps:

  • Define the object type by writing a function.
  • Create an instance of the object with new.

and you basically make both steps in the second case.

游魂 2024-12-29 04:28:12

第一个版本使用对象文字来创建分配给 person 的单个对象实例。它不能像带有 new 的构造函数一样使用来从 person 创建其他对象。

第二个版本使用一个函数,可以将其作为构造函数调用,以使用 new person() 创建新对象。

这就是 new 在 JavaScript 中的工作方式。本质上,如果您希望能够从模板创建多个实例(类似于其他语言中的类的工作方式),请使用 new FunctionName() 语法。如果您只需要创建特定对象的单个实例,则可以使用第一种语法。

请注意,在第一个版本中,您可以说:

person.set('Victor',12);
alert(person.name); // 'Victor'

另请注意,如果您要创建一个旨在用作构造函数的函数,则约定是使用大写字母作为其名称的第一个字母(或中每个单词的第一个字母)姓名)。

The first version uses an object literal to create a single object instance which is assigned to person. This cannot be used like a constructor with new to create other objects from person.

The second version uses a function, which can be called as a constructor to create new objects with new person().

That's just the way new works in JavaScript. Essentially if you want to be able to create multiple instances from a template (similar to how classes work in other languages) then use the new FunctionName() syntax. If you only need to create a single instance of a particular object you can use the first syntax.

Note that in the first version you can say:

person.set('Victor',12);
alert(person.name); // 'Victor'

Note also that if you are creating a function that is intended for use as a constructor the convention is to use uppercase for the first letter of its name (or the first letter of each word in the name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文