派生对象的构造函数与其父对象的构造函数相同 [英] constructor for a derived object is same as that of its parent

查看:63
本文介绍了派生对象的构造函数与其父对象的构造函数相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ECMAScript中学习构造函数的概念。我在代码后面执行
(参见Rhino JavaScript shell中的执行):

函数Foo(a)

{

this.a = a;

}


功能栏(b)

{

this.b = b;

}

Bar.prototype = new Foo(1);

var x = new Foo(2);

var y = new Bar(3);


现在我希望y.constructor给我吧,但我得到了Foo。

有人可以解释吗?


遵循Rhino JavaScript shell中的执行顺序:


Rhino 1.6 release 5 2006 11 18

jsfunction Foo(a)

{

this.a = a;

}

jsfunction酒吧(b)

{

this.b = b;

}

jsBar.prototype = new Foo(1);

[object Object]

jsvar x = new Foo(2);

jsvar y = new Bar(3 );

jsx.constructor


函数Foo(a){

this.a = a;

}


jsy.constru ctor


函数Foo(a){

this.a = a;

}


问候,

Satyajit

解决方案



satyajit写道:


function Foo(a)

{

this.a = a;

}


功能栏(b)

{

this.b = b;

}

Bar.prototype = new Foo(1);

var x = new Foo(2);

var y = new酒吧(3);


现在我希望y.constructor给我吧,但我得到了Foo。



是的......并且在同一个

警报(y instanceof Bar); // true

或(相同但更多概念):

alert(Bar.prototype.isPrototypeOf(y)); // true


整蛊,不是吗? ;-)


我强烈建议您阅读和研究以下所有样本:


Eric Lippert

" ; JScript类型系统,第二部分:原型和构造函数"

< http://blogs.msdn.com/ericlippert/archive/2003/11/06/53352.aspx>


这篇相当简短的博客文章是目前互联网上存在的关于JavaScript

继承的唯一*来源。可以有更多的b
课程,但我两年的搜索没有透露它们。我可以找到的其他任何东西都是错误的废话或多页的启示

让你比以前更加困惑。


PS Eric Lippert是Microsoft JScript引擎的原始制造商。

这并不意味着你必须把他的每一个字都当作最终的

真相:有区别钢琴制造者和钢琴演奏者之间

:-)我只是认为应该提到他不是一些

方ECMAScript专家。


satyajit写道:


我正在尝试学习ECMAScript中构造函数的概念。我在代码后面执行
(参见Rhino JavaScript shell中的执行):

函数Foo(a)

{

this.a = a;

}


功能栏(b)

{

this.b = b;

}

Bar.prototype = new Foo(1);

var x = new Foo(2);

var y = new Bar(3);


现在我希望y.constructor给我吧,但我得到了Foo。

有人可以解释一下吗?



这让很多人感到困惑,但它只表明构造函数

属性不是很直观,分别没有以某种方式设置

遵循人们在基于类的术语中思考的直觉和

期望实例在JavaScript中有一个名为constructor的属性

指向函数构造函数。


您可能想检查

y。 hasOwnProperty(''constructor'')

你会发现y没有该名称的属性。

这意味着当查找y.constructor时原型链是

用于查找属性,因此查找首先查看

Bar.prototype具有构造函数属性但它没有一个含义

Bar.prototype.hasOwnProperty(''constructor'')

也是假的,然后查找Foo.prototype和

Foo.prototype。 hasOwnProperty(''constructor'')

为true,当根据ECMAScript第3版第13.2节创建Foo函数

时,已设置构造函数属性/>
规格。


-


Martin Honnen
http://JavaScript.FAQTs.com/


satyajit写道:
< blockquote class =post_quotes>
我正在尝试学习ECMAScript中构造函数的概念。我在代码后面执行
(参见Rhino JavaScript shell中的执行):

函数Foo(a)

{

this.a = a;

}


功能栏(b)

{

this.b = b;

}

Bar.prototype = new Foo(1);

var x = new Foo(2);

var y = new Bar(3);


现在我希望y.constructor给我吧,但我得到了Foo。

有人可以解释一下吗?



< snip>


当创建一个函数对象时,会给它一个 - prototype - property

并为该属性分配一个值,该值是对象的引用。

然后给该对象一个 - constructor - 赋予

参考的属性函数对象。


如果函数对象用于构造对象,则将当前分配给其 - prototype - 属性的值

分配给新对象

object'的内部[[Prototype]]属性,然后在新对象的原型链中用作

第一个对象。因此这个新对象

继承了对作为构造函数的函数对象的引用

通过其原型链。


你有用Foo实例替换了最初分配给 - Bar.prototype -

的对象。因此,具有 - constructor -

属性的对象已被替换为一个对象,即

继承构造函数 - 通过其原型链的属性 -

Foo.prototype - 。当该对象被分配给使用 - new Bar创建的对象的内部属性时,他们

继承其构造函数 - 属性。


您可以通过以下方式缓解这个问题: -

Bar.prototype = new Foo(1)

Bar.prototype .constructor = Foo;


- 然后新的Bar实例将继承 - 构造函数 - 属性

引用--Foo - 。


但是,你真的不应该编写javascript代码,其中你需要测试任何对象的构造函数(在
实验之外)和测试代码)。

javascript中没有任何投射问题,因为所有对象都是单个类,并且在

松散类型的语言中,如javascript,它是一个必要的学科

程序员要跟踪所使用的类型,所以

需要在运行时查询类型的情况非常特殊。


Richard。


I am trying to learn the concept of constructors in ECMAScript. I
executed following code (See execution in Rhino JavaScript shell):
function Foo(a)
{
this.a = a;
}

function Bar(b)
{
this.b = b;
}
Bar.prototype = new Foo(1);
var x = new Foo(2);
var y = new Bar(3);

Now I expect y.constructor to give me Bar, but I am getting Foo.
Can anybody explain?

Following the execution sequence in Rhino JavaScript shell:

Rhino 1.6 release 5 2006 11 18
jsfunction Foo(a)
{
this.a = a;
}
jsfunction Bar(b)
{
this.b = b;
}
jsBar.prototype = new Foo(1);
[object Object]
jsvar x = new Foo(2);
jsvar y = new Bar(3);
jsx.constructor

function Foo(a) {
this.a = a;
}

jsy.constructor

function Foo(a) {
this.a = a;
}

Regards,
Satyajit

解决方案


satyajit wrote:

function Foo(a)
{
this.a = a;
}

function Bar(b)
{
this.b = b;
}
Bar.prototype = new Foo(1);
var x = new Foo(2);
var y = new Bar(3);

Now I expect y.constructor to give me Bar, but I am getting Foo.

Yep... And at the same
alert(y instanceof Bar); // true
or (the same but more "conceptual"):
alert(Bar.prototype.isPrototypeOf(y)); // true

Tricky, is not it? ;-)

I highly suggest you to read and study all samples from:

Eric Lippert
"The JScript Type System, Part Two: Prototypes and constructors"
<http://blogs.msdn.com/ericlippert/archive/2003/11/06/53352.aspx>

This rather short blog post is the *only one* source about JavaScript
inheritance currently existing in the Internet. There can be more of
course, but my two years search did not reveal them. Anything else I
could find is either an erroneus crap or multi-paged revelations
leaving you even more confusing then ever before.

P.S. Eric Lippert is the original maker of Microsoft JScript engine.
That doesn''t mean that you have to take each his word as the final
truth: there is a difference between a piano maker and a piano player
:-) I just thought that it should be mentioned that he is not some
"side ECMAScript specialist".


satyajit wrote:

I am trying to learn the concept of constructors in ECMAScript. I
executed following code (See execution in Rhino JavaScript shell):
function Foo(a)
{
this.a = a;
}

function Bar(b)
{
this.b = b;
}
Bar.prototype = new Foo(1);
var x = new Foo(2);
var y = new Bar(3);

Now I expect y.constructor to give me Bar, but I am getting Foo.
Can anybody explain?

This has confused a lot of people but it only shows that the constructor
property is not very intuitive respectively not set in a way that
follows the intuition people have that think in class based terms and
expect "instances" in JavaScript to have a property named constructor
that points to the function constructor.

You might want to check
y.hasOwnProperty(''constructor'')
and you will find that y does not have an own property of that name.
That means when y.constructor is looked up that the prototype chain is
used to look for the property and so the lookup first looks at
Bar.prototype having a constructor property but it does not have one meaning
Bar.prototype.hasOwnProperty(''constructor'')
is false too, then follows the lookup of Foo.prototype and
Foo.prototype.hasOwnProperty(''constructor'')
is true and that constructor property has been set when the Foo function
was created according to section 13.2 of the ECMAScript edition 3
specification.

--

Martin Honnen
http://JavaScript.FAQTs.com/


satyajit wrote:

I am trying to learn the concept of constructors in ECMAScript. I
executed following code (See execution in Rhino JavaScript shell):
function Foo(a)
{
this.a = a;
}

function Bar(b)
{
this.b = b;
}
Bar.prototype = new Foo(1);
var x = new Foo(2);
var y = new Bar(3);

Now I expect y.constructor to give me Bar, but I am getting Foo.
Can anybody explain?

<snip>

When a function object is created it is given a - prototype - property
and that property is assigned a value that is a reference to an object.
That object is then given a - constructor - property that is assigned a
reference to the function object.

If the function object is used to construct an object the value
currently assigned to its - prototype - property is assigned to the new
object''s internal [[Prototype]] property, which is then used as the
first object in the new object''s prototype chain. Thus this new object
inherits a reference to the function object used as its constructor
through its prototype chain.

You have replaced the object originally assigned to - Bar.prototype -
with an instance of Foo. Thus the object that had the - constructor -
property that referred to Bar has been replaced with an object that is
inheriting a constructor - property through its prototype chain from -
Foo.prototype -. And when that object is assigned to the internal
[[Prootype]] properties of objects created with - new Bar - they
inherit its - constructor - property.

You can mitigate that issue by doing:-

Bar.prototype = new Foo(1)
Bar.prototype.constructor = Foo;

- and then new instances of Bar will inherit a - constructor - property
that refers to - Foo -.

However, you really should not be writing javascript code in which you
need to test for the constructor of any objects (outside of
experimental and testing code). There are no casting issues in
javascript as all objects are really of a single ''class'' and in a
loosely typed language like javascript it is a necessary discipline for
the programmer to be keeping track of the types being used and so
situations where you need to query a type at runtime are exceptional.

Richard.


这篇关于派生对象的构造函数与其父对象的构造函数相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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