[英]Calling base class function - class inheritance in JavaScript
Please check out the following example: 请查看以下示例:
MyBaseClass = function(a) {
this.a = a;
};
$.extend(MyBaseClass.prototype, {
init: function() {
console.log('I am initializing the base class');
}
});
MyChildClass = $.extend(MyBaseClass, {
init: function() {
MyBaseClass.prototype.init();
console.log('I am initializing the child class');
}
});
var = new MyChildClass();
var.init();
Тhis should output both 'I am initializing the base class' and 'I am initializing the child class'. 他应该输出“我正在初始化基类”和“我正在初始化子类”。
I need to be able to inherit the class MyBaseClass, but still to be able to call his init() method at the beginning of the new init() method. 我需要能够继承MyBaseClass类,但仍要能够在新的init()方法开始时调用其init()方法。
How do I do that? 我怎么做?
jQuery's extend doesn't build inheritance but "Merge the contents of two or more objects together into the first object" . jQuery的扩展不能建立继承,而是“将两个或多个对象的内容合并到第一个对象中” 。
Use prototype based inheritance to achieve your inheritance and explicitly call the "super" method : 使用基于原型的继承来实现您的继承,并显式调用“ super”方法:
MyBaseClass = function(a) {
this.a = a;
};
MyBaseClass.prototype.init = function() {
console.log('I am initializing the base class');
};
MyChildClass = function(a) {
this.a = a;
}
MyChildClass.prototype = Object.create(MyBaseClass.prototype); // makes MyChildClass "inherit" of MyBaseClass
MyChildClass.prototype.init = function() {
MyBaseClass.prototype.init.call(this); // calls super init function
console.log('I am initializing the child class');
};
var child= new MyChildClass();
child.init();
I am initializing the base class
I am initializing the child class
Couple of things. 几件事。 extend
really just adds on properties, it doesn't do much. extend
实际上只是增加了属性,并没有太大作用。 So you need to have a function for your class ready, inherit from the base class, and then use extend on that classes prototype. 因此,您需要为类准备好一个函数,从基类继承,然后在该类原型上使用extend。
function MyChildClass(){};
MyChildClass.prototype = new MyBaseClass();
$.extend(MyChildClass.prototype, {
init: function() {
MyBaseClass.prototype.init();
console.log('I am initializing the child class');
}
});
Here is another approach that I like to use for inheritance - when the specificity of methods is going to be an issue - which is to store the base class in its own property 这是我喜欢用于继承的另一种方法-当方法的特殊性成为问题时-将基类存储在其自己的属性中
function MyChildClass(){};
MyChildClass.prototype = new MyBaseClass();
MyChildClass.prototype.base = new MyBaseClass();
$.extend(MyChildClass.prototype, {
init: function() {
this.base.init();
console.log('I am initializing the child class');
}
});
Another prototype based pattern to achieve this goal: 实现此目标的另一个基于原型的模式:
MyBaseClass = function(a) {
this.a = a;
};
MyBaseClass.prototype = {
init: function() {
console.log('I am initializing the base class');
}
};
MyChildClass = function() {};
MyChildClass.prototype = $.extend(new MyBaseClass(), {
init: function() {
this.super.init.call(this);
console.log('init child');
},
super: MyBaseClass.prototype,
constructor: MyChildClass
});
var a = new MyChildClass();
a.init();
Output: 输出:
I am initializing the base class
init child
Here this.super
stores reference to base class. 这里this.super
存储对基类的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.