JavaScript和继承 [英] JavaScript and inheritence

查看:49
本文介绍了JavaScript和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下使用继承的代码重要的部分是

函数getName()。结果应警告''John Doe'':


函数First(name){

this.name = name;

this.getName = function(){

返回this.name;

}

}


函数Doe(firstName){

this.base = First;

this.base(firsName);


this.getName = function(){

返回this.base.getName()+''Doe'';

}

}

d =新的Doe(''John'');

alert(d.getName());


但它不起作用,因为它说this.base.getName()不是一个函数。


那可能是,但我怎么能继续这个呢?据我所知,没有

this.super或其他东西。

这样做有什么好方法吗?我想我可以在Doe做以下事情:


this.getParentName = this.getName;

this.getName = function(){

返回this.getParentName()+''Doe'';

}


但这不是很整洁,因为当我从这个对象开始并且我将
应用于该新对象的相同技巧然后我将创建无休止的
递归,最终导致堆栈溢出。有人知道吗?


提前致谢,

Vincent

Hi everyone,

I have the following code using inheritence The important part is the
function getName(). The result should alert ''John Doe'':

function First(name) {
this.name = name;

this.getName = function() {
return this.name;
}
}

function Doe(firstName) {
this.base = First;
this.base(firsName);

this.getName = function() {
return this.base.getName()+'' Doe'';
}
}
d = new Doe(''John'');
alert(d.getName());

But it doesn''t work, because it says this.base.getName() is not a function.

That could be, but how can I still accompish this? There is no
this.super or something as far as I know. Is there a nice way of
doing this. I figured I could do the following in Doe:

this.getParentName = this.getName;
this.getName = function() {
return this.getParentName()+'' Doe'';
}

But thats not very neat because when I inhert from this object and I
apply the same trick to that new object then I''ll create neverending
recursion, finally causing a stackoverflow. Anyone know any other way?

Thanks in advance,
Vincent

推荐答案

Vincent van Beveren说:
Vincent van Beveren said:

大家好,

我有以下使用继承的代码重要的部分是
function getName()。结果应警告''John Doe'':

函数First(name){
this.name = name;

this.getName = function() {
返回this.name;
}

功能Doe(firstName){
this.base = First;
这个。碱(firsName);


1.firsName未定义。您应该始终将

测试代码复制并粘贴到您的帖子中,以避免您的示例中的拼写错误

不属于您的原始问题。


2.你想要那条线做什么?你正在调用

一个没有new的构造函数。关键字。

this.getName = function(){
返回this.base.getName()+''Doe'';
}

Hi everyone,

I have the following code using inheritence The important part is the
function getName(). The result should alert ''John Doe'':

function First(name) {
this.name = name;

this.getName = function() {
return this.name;
}
}

function Doe(firstName) {
this.base = First;
this.base(firsName);
1. "firsName" is undefined. You should always copy and paste
tested code into your post, to avoid typos in your example
that aren''t part of your original problem.

2. What do you intend for that line to do? You''re invoking
a constructor without the "new" keyword.

this.getName = function() {
return this.base.getName()+'' Doe'';
}




此时,this.base是对First函数的引用,

不是First类型的对象。


好像你想要你的函数Doe()是:


函数Doe(firstName){

this.base = new First(firstName);

this.getName = function(){

返回this.base.getName()+''Doe'';

}

}


这里没有什么东西可以继承。

它只是封装了一个对象。



At this point, this.base is a reference to the First function,
not an object of type First.

It seems as if you want your function Doe() to be:

function Doe(firstName) {
this.base = new First(firstName);
this.getName = function() {
return this.base.getName()+'' Doe'';
}
}

Nothing is really being inherited here, though.
It''s just encapsulating an object.


在文章< 40 ********************** @ news.xs4all.nl> ;, Vincent van

Beveren< vi ***** @ provident.remove.this.nl>写道
In article <40**********************@news.xs4all.nl>, Vincent van
Beveren <vi*****@provident.remove.this.nl> writes
大家好,

我有以下代码使用继承
< snip>但它不起作用,因为它说this.base。 getName()不是函数。
Hi everyone,

I have the following code using inheritence <snip>But it doesn''t work, because it says this.base.getName() is not a function.



< snip>


也许这个例子


< URL:http://www.jgharris.demon.co.uk/jsfeats/JSfeats.html#seca1p1>


会给你一些想法。


John

-

John Harris


<snip>

Maybe this example

<URL:http://www.jgharris.demon.co.uk/jsfeats/JSfeats.html#seca1p1>

will give you some ideas.

John
--
John Harris


> 1.firsName未定义。您应该始终将
> 1. "firsName" is undefined. You should always copy and paste
测试的代码复制并粘贴到您的帖子中,以避免您的示例中的拼写错误。这不是您原始问题的一部分。


哎呀,是的,我做了一些明确的修改。显然我

忘了测试它。这给出了我遇到问题的错误。


< SCRIPT LANGUAGE =" JavaScript">

函数First(name){

this.name = name;


this.getName = function(){

返回this.name;

}

}


函数Doe(firstName){

this.base = First;

this.base(firstName);


this.getName = function(){

返回this.base.getName()+'' Doe'';

}

}

d =新的Doe(''John'');

alert (d.getName());

< / SCRIPT>

2.你想要那条线做什么?你正在调用没有new的构造函数。关键字。

[...]
这里没有任何东西可以继承。
它只是封装了一个对象。
tested code into your post, to avoid typos in your example
that aren''t part of your original problem.
Oops, yeah, I made some modifications for clearity. Apparently I
forgot to test it. This gives the error I''m having trouble with.

<SCRIPT LANGUAGE="JavaScript">
function First(name) {
this.name = name;

this.getName = function() {
return this.name;
}
}

function Doe(firstName) {
this.base = First;
this.base(firstName);

this.getName = function() {
return this.base.getName()+'' Doe'';
}
}
d = new Doe(''John'');
alert(d.getName());
</SCRIPT>
2. What do you intend for that line to do? You''re invoking
a constructor without the "new" keyword.
[...]
Nothing is really being inherited here, though.
It''s just encapsulating an object.




不,base是JavaScript规范的官方部分。试试这个:


函数Foo(){

this.test =''我来自基类'';

}


功能栏(){

this.base = Foo;

this.base();

}


b =新酒吧();

提醒(b.test);


谢谢,

Vincent



No, base is an official part of the JavaScript specification. Try this:

function Foo() {
this.test = ''Im from the base class'';
}

function Bar() {
this.base = Foo;
this.base();
}

b = new Bar();
alert(b.test);

Thanks,
Vincent


这篇关于JavaScript和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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