Javascript原型链接超类构造函数和方法调用
Javascript Prototype Chaining super class constructor and method calling
我是JavaScript世界的新手,当我尝试原型链继承时,我遇到了这个奇怪的问题。
我有3个类
//class parent
function parent(param_1){
this.param = param_1;
this.getObjWithParam = function(val){
console.log("value in parent class "+val);
console.log("Constructor parameter : "+this.param);
};
};
//class child
function child(param_1){
this.constructor(param_1);
this.getObjWithParam = function(val){
console.log("value in child class "+val);
val = Number(val)+1;
child.prototype.getObjWithParam.call(this, [val]);
};
};
child.prototype = new parent();
//class grandChild
function grandChild(param_1){
this.constructor(param_1);
};
grandChild.prototype = new child();
var gc = new grandChild(666);
gc.getObjWithParam(0);
首先,我想传递一个参数给父类的构造函数,就像在其他OO语言中调用super(args)一样。因此,this.constructor(param_1);
完全符合目的。
但是,输出显示为
value in parent class 0
Constructor parameter : 666
这表明,类的孙子已经跳过了原型链,而不是调用getObjWithParam()的子()类,调用getObjWithParam()的父类。
有人知道这里出了什么问题吗?
注:我还想补充两个发现,第二个是最重要的。->如果我试图通过
找到孙子类的构造函数console.log(gc.constructor)
我得到的输出是
function parent(param_1){
this.param = param_1;
this.getObjWithParam = function(val){
console.log("value in parent class "+val);
console.log("Constructor parameter : "+this.param);
};
}
这不是我所期望的。
->如果我尝试在child()和grandChild()类中注释 //this.constructor(param_1);
,代码将完全按照预期工作。
谁能解释一下这个现象?
另外,如果有人能提出一个解决办法,我将不胜感激。
谢谢
声明一个this。构造函数中的SOME_METHOD不会将其添加到类型的原型中。原型函数需要单独声明,例如:
//class parent
function parent(param_1){
console.log("parent " + param_1);
this.param = param_1;
}
parent.prototype.getObjWithParam = function(val) {
console.log("value in parent class "+val);
console.log("Constructor parameter : "+this.param);
};
//class child
function child(param_1){
console.log("child " + param_1);
this.constructor(param_1);
}
child.prototype = new parent();
child.prototype.getObjWithParam = function(val) {
console.log("value in child class "+val);
val = Number(val)+1;
parent.prototype.getObjWithParam.call(this, [val]);
}
//class grandChild
function grandChild(param_1){
console.log("grandChild " + param_1);
this.constructor(param_1);
}
grandChild.prototype = new child();
var gc = new grandChild(666);
gc.getObjWithParam(0);
我建议您阅读这篇文章,以更深入地了解原型在javascript中是如何工作的。
如果你想在jQuery中做链接(流畅接口):
<div id="div"></div>
<script type="text/javascript">
function $(id) {
if(this.$) return new $.init(id);
}
$.init = function(id) {
if(typeof id == 'string') {
this.id = document.getElementById(id);
}
};
$.init.prototype = $.prototype = {
constructor: $,
css: function(value) {
for(i in value) {
this.id.style[i] = value[i];
}
return this;
},
mush : function() {
var text = this.id.innerHTML;
this.id.innerHTML = text.split('').join('--');
return this;
},
text : function(a) {
this.id.innerHTML = a;
return this;
}
};
$('div').text('FOO').mush().css({
'color' : 'red',
'textTransform' : 'uppercase'
});
</script>
看到示例
相关文章:
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- javascript从子方法调用父方法
- 我应该返回一个类似console.log()的方法调用吗
- NodeJS-从同一文件中的另一个方法调用一个方法
- 使用restangular save方法调用put与post时的控件
- 从实例方法调用实例方法
- Node.js-异步方法调用问题
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- 向模板实例变量传递调用方法调用的结果时出现异常
- jQuery 方法调用了两次
- 不能从 Backbone.View 的方法调用 Backbone.collection 的方法,其中包含 require
- 组合 onLayout 的方法调用
- lodash/下划线 - 使用绑定与方法调用和方法保持未绑定
- 从公共方法调用模块的私有函数
- 如何对 jqGrid 的 loadComplete 和 onSelectRow 进行基因预和后方法调用
- 完全限定的 JavaScript 方法调用
- 为什么我在 Angular JS 中收到两个方法调用
- 如何在从排序方法调用参数时将其传递给回调
- 使用两种不同的方法调用方法
- 如何在 javascript 中更改方法调用的全局范围