在JavaScript中继承 [英] inheritance in JavaScript

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

问题描述

问:是否可以从本机对象继承?

我试图使用Date对象作为基础对象,如下所示:


x.prototype = Date.prototype;

x.prototype.constructor = x;


函数x(){

//构造函数

}


var obj = new x();


错误---- > x.getDate();


有另一种方式吗?

解决方案


sh ************* @ gmail.com 写道:

问:是否可以从本机对象继承?


sh ********** ***@gmail.com 写道:问:是否可以从本机对象继承?




JavaScript / JScript本机对象被保护 ;在最好的意思是

的OOP。您最初可以通过以下操作来欺骗构造函数:

....

this.getTime = Date.prototype.getTime;

.. ..

但它会在第一次方法调用时失败(调用者对象类型不会与
匹配)。


同样没有太多意义来延长日期,因为它不会是b $ b自动更新时间:它将始终是创建的时间。因此它更好地对待:


函数XDate(){

this.getTime = function(){

return(new Date())。getTime();

}

//我的其他方法

}


对于内存管理安全:


函数XDate(){

这个。 getTime = function(){

var d = new Date();

return d.getTime();

}

//我的其他方法

}


sh ************* @ gmail.com 写道:

问:是否可以从本机对象继承?


仅来自Object的AFAIK。

我试图使用Date对象作为基础对象,如下所示:

x.prototype = Date.prototype;


ReferenceError:x未定义。

x.prototype.constructor = x;

函数x(){
//构造函数
}
var obj = new x();

ERROR ----> x.getDate();


如果构造函数优先于其他所有(为了避免ReferenceError

以上),这是理所当然的


TypeError:x.getDate不是函数

还有另外一种方法吗?




从另一个原型继承的一种方法是


函数inheritFrom(o)

{

var Dummy = function(){};

Dummy.prototype = new o();

返回新的Dummy();

}


函数MyDate()

{

//无论

}


MyDate.prototype = inheritFrom(Date);


var d = new MyDate();


这允许像'd'这样的MyDate对象继承Date.prototype.getDate(),

as


alert(d.getDate);


显示。但是,


alert(d.getDate());


结果


TypeError :Date.prototype.getDate在不兼容的对象上调用。


因此无法直接从Date继承并使用

继承的方法。我知道有两种可能的解决方法:


A)在用户定义的对象中封装Date对象


函数MyDate()

{

this.date = new Date();

}


MyDate.prototype。 getDate = function()

{

返回this.date.getDate();

}


// ...


alert(新的MyDate()。getDate());


B)扩展Date原型和使用Date对象:


Date.prototype.isLeapYear = function(y)

{

return(new Date(y) || this.getFullYear(),1,29).getMonth()== 1);

}


alert(new Date()。isLeapYear ());

PointedEars


Q: is it possible to inherit from a native object?

I tried to use Date object as the base object like so:

x.prototype = Date.prototype;
x.prototype.constructor = x;

function x(){
//constructor
}

var obj = new x();

ERROR ----> x.getDate();

is there another way?

解决方案


sh*************@gmail.com wrote:

Q: is it possible to inherit from a native object?

sh*************@gmail.com wrote: Q: is it possible to inherit from a native object?



JavaScript/JScript native objects are "protected" in the best meanning
of OOP. You can initially spoof constructor by doing something like:
....
this.getTime = Date.prototype.getTime;
....
But it will fail on the first method call (caller object type doesn''t
match).

Also there is not too much sense to extend Date as it doesn''t
autoupdate time: it will be always the time of the creation. Thus it is
much better to:

function XDate() {
this.getTime = function() {
return (new Date()).getTime();
}
// my other methods
}


For memory management safety:

function XDate() {
this.getTime = function() {
var d = new Date();
return d.getTime();
}
// my other methods
}


sh*************@gmail.com wrote:

Q: is it possible to inherit from a native object?
AFAIK only from Object.
I tried to use Date object as the base object like so:

x.prototype = Date.prototype;
ReferenceError: x is not defined.
x.prototype.constructor = x;

function x(){
//constructor
}

var obj = new x();

ERROR ----> x.getDate();
If the constructor comes before everything else (to avoid the ReferenceError
above), that is rightfully

TypeError: x.getDate is not a function
is there another way?



One way to inherit from another prototype is

function inheritFrom(o)
{
var Dummy = function() {};
Dummy.prototype = new o();
return new Dummy();
}

function MyDate()
{
// whatever
}

MyDate.prototype = inheritFrom(Date);

var d = new MyDate();

This allows MyDate objects such as `d'' to inherit Date.prototype.getDate(),
as

alert(d.getDate);

shows. However,

alert(d.getDate());

results in

TypeError: Date.prototype.getDate called on incompatible Object.

So it is not possible to inherit from Date directly and use the
inherited methods. There are two possible workarounds I know of:

A) Encapsulate a Date object in the user-defined object

function MyDate()
{
this.date = new Date();
}

MyDate.prototype.getDate = function()
{
return this.date.getDate();
}

// ...

alert(new MyDate().getDate());

B) Extend the Date prototype and use a Date object:

Date.prototype.isLeapYear = function(y)
{
return (new Date(y || this.getFullYear(), 1, 29).getMonth() == 1);
}

alert(new Date().isLeapYear());
PointedEars


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

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