在 JavaScript 中实例化新对象
我可能缺少一些关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的第一个示例是对象文字:
您创建了一个名为 person 的对象,仅此而已。创造新人的概念没有任何意义。
如果您想创建可用于随意创建更多对象的东西,您可以使用一个函数,就像第二个示例一样。
但请注意,按照惯例,打算用作构造函数的函数以大写字母开头。另请注意,当您在构造函数内部设置函数时,使用
此函数会创建一个特权函数,因为它可以访问公共和私有成员。如果此设置函数仅需要访问公共属性,则通常会将其添加到函数的
原型
中。整个事情可能是这样的特权方法可能是这样的
localData
是Person
函数的本地方法,并且不能作为 Person 实例上的属性进行访问;但是,showPrivateData
函数以及您可能添加的任何其他特权函数将在其上形成一个闭包,并可以访问它。最后,请注意构造函数可以带参数:
Your first example was an object literal:
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 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 lookAnd here's what a privileged method might look like
localData
is local to thePerson
function, and cannot be accessed as a property on instances of Person; however, theshowPrivateData
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:
你回答你自己的问题。构造函数(
new
后面的内容)必须是 JavaScript 中的函数。You answer your own question. A constructor (the thing that goes next to
new
) must be a function in JavaScript.JavaScript 没有类(尽管您可以模拟它们),这就是原因。并且
new
仅适用于函数(在本例中,它在函数内部创建特殊变量this
,该变量在函数调用后隐式返回)。因此,当使用它时:
您已经创建了一个对象(该对象具有一个函数作为其属性之一)。
但是,当您使用 this 时:
您仅创建函数,当使用
new
调用该函数时,该函数会返回this
以及所有分配的属性。文档内容
作为 Mozilla 上的
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 variablethis
inside function, which is being returned implicitly after function call).So, when using this:
you already create an object (which has a function as one of its properties).
But when you use this:
you only create function, that returns
this
with all assigned properties when the function is called withnew
.What documentation says
As documentation of
new
operator on Mozilla Developer Network says:and you basically make both steps in the second case.
第一个版本使用对象文字来创建分配给
person
的单个对象实例。它不能像带有new
的构造函数一样使用来从person
创建其他对象。第二个版本使用一个函数,可以将其作为构造函数调用,以使用
new person()
创建新对象。这就是
new
在 JavaScript 中的工作方式。本质上,如果您希望能够从模板创建多个实例(类似于其他语言中的类的工作方式),请使用 new FunctionName() 语法。如果您只需要创建特定对象的单个实例,则可以使用第一种语法。请注意,在第一个版本中,您可以说:
另请注意,如果您要创建一个旨在用作构造函数的函数,则约定是使用大写字母作为其名称的第一个字母(或中每个单词的第一个字母)姓名)。
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 withnew
to create other objects fromperson
.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 thenew 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:
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).