为什么在javascript中列出一个类的实际构造函数很重要 [英] why does listing the actual constructor of a class in javascript important

查看:146
本文介绍了为什么在javascript中列出一个类的实际构造函数很重要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读javascript花园 http://bonsaiden.github.com/JavaScript-Garden/ 关于原型在javascript中,其示例之一是这样的:

  function Foo(){
this.value = 42;
}
Foo.prototype = {
method:function(){}
};

function Bar(){}

//将Bar的原型设置为Foo的新实例
Bar.prototype = new Foo();
Bar.prototype.foo ='Hello World';

//确保列出Bar作为实际构造函数< -------------------
Bar.prototype.constructor =酒吧;

请注意,确保列出Bar作为实际构造函数。我真的很迷失这是什么/是。我试着做了新的实例的Bar()有和没有最后一行。但是在这些实例上调用value或method返回完全相同的东西。所以我不知道,什么需要(我认为必须有一个)指定构造函数?



谢谢!!!

原型属性,它在创建函数对象时被赋值,它指向一个新创建的继承自 Object.prototype 的对象,它有一个构造函数属性,它简单地指向函数本身。



原型属性的目的是提供一种使用构造函数实现继承的方法。当你使用 new 运算符调用一个函数时,它会创建一个新的对象,继承该构造函数的原型



现在,构造函数属性的目的是有一种方法来引用回创建对象,例如:

  function Foo(){} 
//属性的默认值:
Foo.prototype.constructor == Foo; // true

此属性由 Foo ,所以你可以知道哪个构造函数用来创建一个对象:

  var foo = new Foo ); 
foo.constructor == Foo;

如果将一个新对象分配给函数的原型,这种关系就会丢失:

  function Bar(){} 
Bar.prototype = {inherited:1};

Bar.prototype.constructor == Bar; // false
Bar.prototype.constructor == Object; // true

它还影响函数的实例:

  var bar = new Bar(); 
bar.constructor == Bar; // false
bar.constructor == Object; // true

另一个类似的情况是,当使用构造函数具有两个或多个级别的继承时,常用的方式用于表示函数之间的继承关系,是指定第二级的原型属性,例如:

  function Parent(){} 

function Child(){}
Child.prototype = new Parent

上面的代码有几个问题,首先,它执行父构造函数的逻辑来创建继承关系,但这是另一个故事,在上面的例子中,构造函数属性也受到影响,因为我们完全替换 Child.prototype object:

  var child = new Child 
child.constructor == Parent; // true

如果我们替换构造函数属性 Child.prototype 分配后,将显示预期的行为:

  function Child(){} 
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child = new Child();
child.constructor == Child; // true


I was reading on javascript garden http://bonsaiden.github.com/JavaScript-Garden/ about prototype in javascript and one of its example goes like this:

function Foo() {
    this.value = 42;
}
Foo.prototype = {
    method: function() {}
};

function Bar() {}

// Set Bar's prototype to a new instance of Foo
Bar.prototype = new Foo();
Bar.prototype.foo = 'Hello World';

// Make sure to list Bar as the actual constructor <-------------------
Bar.prototype.constructor = Bar;

Notice the line that reads Make sure to list Bar as the actual constructor. I really am lost about what this does/is. I have tried making new instances of Bar() with and without the last line. But call "value" or "method" on those instances return the exact same things. So I wonder, what's the need (I assume there must be one) of specifying the constructor?

Thank You!!!

解决方案

Every function has a prototype property, it's assigned when the function object is created, it points to a newly created object that inherits from Object.prototype, and it has a constructor property, that simply points back to the function itself.

The purpose of the prototype property is to give a way to implement inheritance, using constructor functions. When you call a function with the new operator, it will create a new object that inherits from that constructor's prototype.

Now, the purpose of the constructor property is to have a way to refer back to the constructor that has created an object, for example:

function Foo () {}
// default value of the property:
Foo.prototype.constructor == Foo; // true

This property is inherited by the "instances" of Foo, so you can know which constructor was used to create an object:

var foo = new Foo();
foo.constructor == Foo;

If you assign a new object to the function's prototype, this relationship is lost:

function Bar () {}
Bar.prototype = { inherited: 1 };

Bar.prototype.constructor == Bar;    // false
Bar.prototype.constructor == Object; // true

And it affects also the instances of the function:

var bar = new Bar();
bar.constructor == Bar;    // false
bar.constructor == Object; // true

Another similar case is when you have two or more levels of inheritance using constructors, the most common way is used to denote the inheritance relationship between the functions, is to assign the prototype property of the second level, e.g.:

function Parent() {}

function Child () {}
Child.prototype = new Parent();

The above code has several problems, first, it executes the logic of the parent constructor to create the inheritance relationship, but that's another story, in the above example the constructor property is also affected, since we replace completely the Child.prototype object:

var child = new Child();
child.constructor == Parent; // true

If we replace replace the value of the constructor property of Child.prototype after assigning it, it will show the expected behavior:

function Child() {}
Child.prototype = new Parent();
Child.prototype.constructor = Child;

var child = new Child();
child.constructor == Child; // true

这篇关于为什么在javascript中列出一个类的实际构造函数很重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆