使js具有继承能力的方法 [英] a method to make js have the ability to inherit

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

问题描述

我找到一种方法让javescript更像c ++或pyhon

有示例代码:


function Father(self)//每个构造函数都可能有自我

参数

{

self = self?self:this; //每个班级都有这个陈述


self.hello = function()

{

alert(" father" + self.name);

}

self.name =" baibai";

}


function Child(self)//每个构造函数都可能有self争论

{

self = self?self:this; //每个班级都有这个陈述


//从faher继承

父亲(个体经营);

self.hello = function()

{

alert(" child" + self.name);

}

}


a =新父亲();

a.hello();

b = new Child();

b.hello();

Hi,I find a way to make javescript more like c++ or pyhon
There is the sample code:

function Father(self) //every contructor may have "self"
argument
{
self=self?self:this; //every class may have this statement

self.hello = function()
{
alert("father"+self.name);
}
self.name = "baibai";
}

function Child(self) //every contructor may have "self" argument
{
self=self?self:this; //every class may have this statement

//inherit from faher
Father(self);
self.hello = function()
{
alert("child"+self.name);
}
}

a = new Father();
a.hello();
b = new Child();
b.hello();

推荐答案

zz *********** @ yahoo.com.cn 写道:

我找到一种方法让javescript更像c ++或pyhon

有示例代码:


function父亲(自我)//每一个构造函数可以具有自我。争论

{

self = self?self:this; //每个班级都有这个陈述


self.hello = function()

{

alert(" father" + self.name);

}

self.name =" baibai";

}


function Child(self)//每个构造函数都可能有self

参数{

self = self?self:this; //每个班级都有这个陈述


//从faher继承

父亲(个体经营);

self.hello = function()

{

alert(" child" + self.name);

}

}


a =新父亲();

a.hello();

b = new Child();

b.hello();
Hi,I find a way to make javescript more like c++ or pyhon
There is the sample code:

function Father(self) //every contructor may have "self" argument
{
self=self?self:this; //every class may have this statement

self.hello = function()
{
alert("father"+self.name);
}
self.name = "baibai";
}

function Child(self) //every contructor may have "self"
argument {
self=self?self:this; //every class may have this statement

//inherit from faher
Father(self);
self.hello = function()
{
alert("child"+self.name);
}
}

a = new Father();
a.hello();
b = new Child();
b.hello();



但是你使用一小部分代码并使用

javascript继承所取得的成就。 >
大大减少了复杂性?例如,如下: -


函数父亲(){

}

Father.prototype = {

你好:function(){

alert(this.type + this.name);

};

type:"父亲;

名字:父亲

};

函数Child(){

}

Child.prototype = new父():

Child.prototype.type =''child'';

a = new Father();

a.hello();

b = new Child();

b.hello();


Richard。

But have you achieved anything more than can be done with normal
javascript inheritance, using a fraction of the code and with
considerably less added complexity? For example, as:-

function Father(){
}
Father.prototype = {
hello:function(){
alert(this.type+this.name);
};
type:"father";
name:"father"
};
function Child(){
}
Child.prototype = new Father():
Child.prototype.type = ''child'';
a = new Father();
a.hello();
b = new Child();
b.hello();

Richard.


Richard Cornford写道:

< snip>
Richard Cornford wrote:
<snip>

function父亲(){

}

Father.prototype = {

你好:function(){

alert(this.type + this.name);

};

type:" father" ;

名称:父亲

};
function Father(){
}
Father.prototype = {
hello:function(){
alert(this.type+this.name);
};
type:"father";
name:"father"
};



< snip>


对象文字中的名称值对应以逗号分隔,而不是以
分号分隔。 IE: -


Father.prototype = {

hello:function(){

alert(this.type + this。姓名);

},

类型:父亲,

姓名:" father"

理查德。

<snip>

The name value pairs in the object literal should be comma separated not
semicolon separated. I.E:-

Father.prototype = {
hello:function(){
alert(this.type+this.name);
},
type:"father",
name:"father"
};

Richard.


文章< 11 ************** ********@j44g2000cwa.googlegroups .com> ;,
zz ** *********@yahoo.com.cn
In article <11**********************@j44g2000cwa.googlegroups .com>,
zz***********@yahoo.com.cn writes

>我找到一种让javescript更像c ++的方法或pyhon
>Hi,I find a way to make javescript more like c++ or pyhon



为什么?


John

-

John Harris

Why ?

John
--
John Harris


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

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