English 中文(简体)
j Script Script
原标题:Intriguing behavior when set prototype on Constructor in javaScript

首先,我用简单的尝试界定了一名行凶者,并利用他来创建一种榜样:myobject<>code:

function MyConstructor () {};
var myobject = new MyConstructor ();

然后,我修改了<代码>prototype该信使的财产,并创建了另一个实例myobject1。 随函附上:

MyConstructor.prototype = {};
var myobject1 = new MyConstructor ();

我重复了同样的程序,并创建了另一个案例:myobject2:

MyConstructor.prototype = {name:  2 };
var myobject2 = new MyConstructor ();

Now I am testing the constructor property of each instance, which is not what I expect:

myobject.constructor == MyConstructor;
//true
myobject1.constructor == MyConstructor;
//false
myobject2.constructor == MyConstructor;
//false

在研究[[类型]时,它彼此不同。

myobject.__proto__ == myobject1.__proto__
//false
myobject2.__proto__ == myobject1.__proto__
//false

Could anyone explain what happens to MyConstructor when changing its prototype property?

最佳回答

When you creating constructor:

function MyConstructor() {};

it has prototype.constructor set to MyConstructor:

MyConstructor.prototype.constructor === MyConstructor; //true

当你超标时,我会的练习者被蒙骗。

MyConstructor.prototype = { foo: function () {} };

接着是格伊利·米希赖尔。 原型物体由通过物体{ f:功能(){}}取而代之,该物体没有财产构造,而是为我的核查师设计,而是运行。 目的,因为物体的构造与物体制造的所有物体。

之后:

MyConstructor.prototype = { foo: function () {} };
MyConstructor.prototype.constructor === Object;

以及当你用这个构造制造新物体时

var foo = new MyConstructor();

之后:

foo.constructor === Object; // true

在你超标的原型之后,你必须纠正建筑领域:

MyConstructor.prototype = { foo: function () {} };
MyConstructor.prototype.constructor = MyConstructor;

and then your constructor has prototype with correct constructor field. You could also write:

MyConstructor.prototype = { foo: function () {}, constructor: MyConstructor };

最后效果相同。

问题回答

施工商拥有原型。 销毁了1{}的原型后,你有效地删除了对建筑商的任何提及。





相关问题
UserControl constructor with parameters in C#

Call me crazy, but I m the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such ...

Anonymous class question

I ve a little doubt over this line: An anonymous class cannot define a constructor then, why we can also define an Anonymous class with the following syntax: new class-name ( [ argument-list ] ) {...

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

热门标签