javascript如何创建对象?

时间:2020-12-21 16:47:45
function Person(name) {
    this.name = name;
    this.say = function() {
        console.info('I am ' + this.name);
    }
}
var p=new Person('*');

Someone tell me that the above codes are equals to :

有人告诉我上面的代码等于:

function Person(name) {
    this.name = name;
    this.say = function() {
        console.info('I am ' + this.name);
    }
}

var p={};
Person.call(p,'*');

Is this true?

这是真的?

If so, how about the prototype?

如果是这样,原型怎么样?

Each object in javascripte owns a prototype,and the prototype chain hold the releationship of the obejcts,I wonder if this prototype does something or not.

javascripte中的每个对象都拥有一个原型,原型链保持了对象的关系,我想知道这个原型是否做了什么。

In this example, when the object of 'p' is created,does it call some build-in method of the superclass of Person?

在这个例子中,当创建'p'的对象时,它是否调用了Person的超类的一些内置方法?

BTW,what I want to know is what does the syntax var p=new Person('*'); do ?

顺便说一句,我想知道的是语法var p = new Person('*');做什么?


-----------------update------------------

-----------------更新------------------

function Person(name) {
    this.name = name;
}
Person.prototype.say = function() {
    console.info('I am ' + this.name);
}

How about if I put the defination of say inside the person function:

如果我把说法的定义放在person函数中怎么样:

function Person(name) {
    this.name = name;
    Person.prototype.say = function() {
        console.info('I am ' + this.name);
    }
}

2 个解决方案

#1


2  

The code:

代码:

var p=new Person('*');

creates a new instance of the Person class. You must remember that classes in javascript are functions, so when you call:

创建Person类的新实例。你必须记住,javascript中的类是函数,所以当你调用时:

Person.call(p,'*');

You are just calling the Person function and binding it to the p object (this means that in the function context the this will refer to the p object). Those pieces of code do the same thing, except for the fact that the first is an instance of the class so if you update the prototype object of person its properties will be updated.

您只是调用Person函数并将其绑定到p对象(这意味着在函数上下文中,它将引用p对象)。这些代码片段做同样的事情,除了第一个是类的实例的事实,所以如果你更新person的原型对象,它的属性将被更新。

Anyway the situation is different when you fill the prototype object of the Person function:

无论如何,当您填充Person函数的原型对象时,情况会有所不同:

Person.prototype={test:"test"}

If you add this after the Person function declaration you will see that those pieces of code have a different behaviour. The object initialized with "new" will contain the test property, but the other one hasn't it. This happens because the prototype object is applied only to the instances of the class when using "new".

如果在Person函数声明之后添加它,您将看到这些代码片段具有不同的行为。用“new”初始化的对象将包含test属性,但另一个没有它。发生这种情况是因为原型对象在使用“new”时仅应用于类的实例。

#2


0  

Well, actually

实际上

var p=new Person('*');

is equivalent to:

相当于:

var p={};
p.__proto__ = Person.prototype;
Person.call(p,'*');

except that __proto__ is not standard JavaScript (but is supported by some implementations).

除了__proto__不是标准的JavaScript(但是某些实现支持)。

#1


2  

The code:

代码:

var p=new Person('*');

creates a new instance of the Person class. You must remember that classes in javascript are functions, so when you call:

创建Person类的新实例。你必须记住,javascript中的类是函数,所以当你调用时:

Person.call(p,'*');

You are just calling the Person function and binding it to the p object (this means that in the function context the this will refer to the p object). Those pieces of code do the same thing, except for the fact that the first is an instance of the class so if you update the prototype object of person its properties will be updated.

您只是调用Person函数并将其绑定到p对象(这意味着在函数上下文中,它将引用p对象)。这些代码片段做同样的事情,除了第一个是类的实例的事实,所以如果你更新person的原型对象,它的属性将被更新。

Anyway the situation is different when you fill the prototype object of the Person function:

无论如何,当您填充Person函数的原型对象时,情况会有所不同:

Person.prototype={test:"test"}

If you add this after the Person function declaration you will see that those pieces of code have a different behaviour. The object initialized with "new" will contain the test property, but the other one hasn't it. This happens because the prototype object is applied only to the instances of the class when using "new".

如果在Person函数声明之后添加它,您将看到这些代码片段具有不同的行为。用“new”初始化的对象将包含test属性,但另一个没有它。发生这种情况是因为原型对象在使用“new”时仅应用于类的实例。

#2


0  

Well, actually

实际上

var p=new Person('*');

is equivalent to:

相当于:

var p={};
p.__proto__ = Person.prototype;
Person.call(p,'*');

except that __proto__ is not standard JavaScript (but is supported by some implementations).

除了__proto__不是标准的JavaScript(但是某些实现支持)。