Javascript继承:未启用父属性
Javascript inheritance : parent property not enabled
这是我实现继承的代码:
<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 控制台.log显示继承的父类中的"parent",但现在,它只显示未定义。
我不知道为什么它不继承父属性。
提前感谢您的帮助。
因为你从不调用函数 Parent
,这是设置属性的原因。如果你仔细观察你的inherit
函数,你正在使用P
的prototype
属性,但你永远不会调用P
——这是一件好事;Child
谁应该打电话给P
:
function Child(nameChild) {
Parent.call(this);
console.log("parentprop:" + this.parentName);
}
显然,这样做的缺点是您必须在多个地方 列出Parent
- 无论是在inherit
调用中,还是在Child
中。您可以通过在子函数上设置父构造函数来缓解这种情况 inherit
:
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;
}
}());
。那么Child
就变成了:
function Child(nameChild) {
Child.parent.call(this);
console.log("parentprop:" + this.parentName);
}
如果你有兴趣彻底地做层次结构(支持调用"超级"方法等(,你可能想看看我的Lineage
工具包。这是一个小工具包,可以自动化很多这些东西,但如果你想了解这些东西是如何工作的,源代码可能会成为有趣的阅读。那里还有一个 wiki 页面,显示了不使用Lineage
的多层、全功能继承(作为将其与使用 Lineage
进行比较的一种手段(。
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 我应该何时在WebGL/OOpenGL中启用/禁用顶点位置属性
- 无法设置未定义或空引用的属性“ajax已启用”
- 如何在 Office 365 加载项中启用允许从沙盒属性顶部导航
- 如何动态获取输入类型=“text”的 name 属性并使用 JQuery 禁用或启用它
- Javascript继承:未启用父属性
- 如何在chrome浏览器中的输入字段中使用只读属性时启用光标移动
- “没有VBO绑定到启用的顶点属性索引0”试图在WebGL中绘制一个简单的纹理
- JavaScript 启用新的 HTML5 按钮属性:formaction、formmethod
- 在启用(“click”)的情况下获取单击的元素属性
- 根据“数据”属性启用或禁用复选框
- Jquery-Mobile:如何在外部java脚本中使用name属性启用选择标签
- HTML选择启用禁用属性
- 在Nashorn中启用乐观类型的嵌套对象中出现奇怪的行为(属性设置为零)
- 未启用ES7属性初始化器的柯里函数
- 启用不同类型的脚本标记?仅更改类型属性是不够的
- 尝试在地图框/传单中启用标记拖动时,无法读取 null 的属性“_leaflet_mousedown5”
- 如何使用启用属性绑定数据
- 禁用基于模型属性的启用下拉列表 在 mvc 中
- Javascript的href属性在修改后没有显示或启用