关于js继承的一个小问题

ShoedPC 2011-12-05 09:51:23
废话少说了,直接上代码:

/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be revritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
*/
var inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};

这个是nodejs中用来继承的,小弟没看明白怎么回事,关于Object.create的资料又比较的少,求教。
...全文
100 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainsilence 2011-12-05
  • 打赏
  • 举报
回复
你的这段代码挺好,我给收藏到我的blog了,呵呵。。。但是现存的很多浏览器还是不能用这个方法,挺遗憾
rainsilence 2011-12-05
  • 打赏
  • 举报
回复
关于Object.create的解释,你可以参考我的blog中的第一第二条
http://rainsilence.iteye.com/blog/892002

你这段话的大意为:
var inherits = function(ctor, superCtor) {
// 将superCtor赋值给.super便于将来利用父类的构造体
ctor.super_ = superCtor;
// 这句话几乎等同于ctor.prototype = new superCtor
// 不同点在于,他在返回值上增加了constructor属性,并且将它的值赋成ctor,不可列举,可更改,可配置,可删除。
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧