Javascript继承:未启用父属性
以下是实现继承的代码:Javascript继承:未启用父属性,javascript,Javascript,以下是实现继承的代码: <html lang="en"> <head> <title>JavaScript Patterns</title> <meta charset="utf-8"> </head> <body> <script> /* Title: Classical Pattern #5 - A Temporary Constructor (a patt
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Classical Pattern #5 - A Temporary Constructor (a pattern that should be generally avoided)
Description: first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
*/
/* Basic */
/*function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
}*/
/* Storing the Superclass */
/*function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
}*/
/* Resetting the Constructor Pointer */
/*function inherit(C, P) {
var F = function () {};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}*/
/* in closure */
var inherit = (function () {
var F = function () {
};
return function (C, P) {
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}
}());
function Parent(name) {
this.nameParent = name || 'Adam';
this.parentName = "parent";//this.nameParent;
}
// adding functionality to the prototype
Parent.prototype.say = function () {
return this.nameParent;
};
// child constructor
function Child(nameChild) {
console.log("parentprop:" + this.parentName);
}
inherit(Child, Parent);
var kid = new Child();
console.log(kid.name); // undefined
console.log(typeof kid.say); // function
kid.nameParent = 'Patrick';
console.log(kid.parentName);
console.log(kid.say()); // Patrick
console.log(kid.constructor.nameParent); // Child
console.log(kid.constructor === Parent); // false
// reference
// http://shop.oreilly.com/product/9780596806767.do
</script>
</body>
我需要Child console.log来显示继承的父类的父类,但现在,它只显示未定义的
我不知道为什么它不是从父属性继承的
提前感谢您的帮助。因为您从未调用函数Parent,这是设置属性的原因。如果你仔细观察你的继承函数,你正在使用P的原型属性,但是你从来没有调用过P——这是一件好事;应该叫P的是孩子: 显然,这有一个缺点,即您必须在多个位置列出父项—既在继承调用中,也在子项中。您可以通过在inherit中的子函数上设置父构造函数来缓解此问题:
如果您对完全支持调用超级方法之类的方法来实现层次结构感兴趣,您可能想看看我的。这是一个小的工具包,可以自动处理这些东西,但是如果你想了解这些东西是如何工作的,那么这个源代码可能会让你读起来很有趣。还有一个展示了多层、功能齐全的继承,没有使用谱系作为与使用谱系进行比较的手段。感谢这一点,但C.uber必须更改为C.parent.prototype=p.prototype;否?我也将在血统中进行调查。uber旨在作为超级或父访问器。@Alphapage:uber旨在作为超级或父访问器。如果是,只需设置uber=P;如果您需要父原型而不是父构造函数,请使用C.uber.prototype。或者两者兼有,为简洁起见,尽管有一点不同,如果你总是参考P.prototype,并且有人替换而不是扩充P.prototype,你总是会得到更新版本。当然,如果有人这样做,它将不再与支持实例的原型匹配。。。
function Child(nameChild) {
Parent.call(this);
console.log("parentprop:" + this.parentName);
}
var inherit = (function () {
var F = function () {
};
return function (C, P) {
F.prototype = P.prototype;
C.parent = P; // <==== The new bit
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}
}());
function Child(nameChild) {
Child.parent.call(this);
console.log("parentprop:" + this.parentName);
}