JavaScript:使用子类中的父方法
JavaScript: Use parent methods from child class?
我正在尝试使用子类中父类的方法。在其他语言中,您只需要使用 extends
并且父级的所有方法都可以在子语言中使用,但在 JavaScript 中,它似乎有所不同。
function Svg() {
this.align = function(value) {
if(value === 'left') return 0;
}
}
function Graph() {
// I want to align text to the left
console.log('<text x="' + align('left') + '"></text>');
}
graph = new Graph();
Graph.prototype = new Svg();
Graph.prototype.align.call();
http://jsfiddle.net/cBMQg/9/
我知道下面的代码不一定像其他 OOP 语言那样"扩展"。但它确实需要另一个函数/类作为属性 - 您可以直接调用它的方法。此外,我还没有在这个演示中使用JavaScript原型。
<script>
function Svg() {
this.align = function( align ) {
if( align == 'left')
return 'left';
else
return 0;
}
}
function Graph() {
this.Svg = new Svg();
// I want to align text to the left
this.AlignLeft = function( direction ) {
console.log('<text x="' + this.Svg.align( direction ) + '"></text>');
}
}
graph = new Graph();
graph.AlignLeft('left'); //console.log <text x="left"></text>
</script>
小提琴:http://jsfiddle.net/cBMQg/11/
function Svg() {
this.align = function(value) {
if(value === 'left') return 0;
}
}
function Graph() {
Svg.call(this); // Call the Super Class Constructor with changed THIS
// I want to align text to the left
console.log('<text x="' + align('left') + '"></text>');
}
graph = new Graph();
Graph.prototype = new Svg();
graph.align('left');
答案可能有效,但为什么不使用原型?对齐函数是否会在每个实例上执行不同的逻辑?
正如伯吉指出的那样;JavaScript 使用原型进行继承,最好在原型上定义在实例之间不会更改的成员:
简单地解释;原型可用于声明不会更改实例的成员/属性。如果我声明一个名为 Person 的对象,并且 person 有 2 个成员:name 和 greet。问候语将输出"你好,我是 [this.name]",因此问候语不会在实例之间更改。
当我在 Person 原型上声明 greet 方法,然后创建数千个 Person 实例(ben、jack、mary ...(时,它们都将只共享一个greet
函数。这样可以节省内存和 CPU 时间以进行对象初始化。查看此链接以获取更多信息: https://stackoverflow.com/a/16063711/1641941
以下链接可能会帮助您了解 JavaScript 中this
所指的内容。 https://stackoverflow.com/a/19068438/1641941
function Svg() {
this.someInstanceValue=22;
}
Svg.prototype.align = function(value) {
if(value === 'left') return 0;
}
function Graph() {
// get Svg's instance properties
Svg.apply(this,arguments);
console.log('<text x="' + this.align('left') + '"></text>');
}
//inherit from Svg:
Graph.prototype=Object.create(Svg.prototype);
Graph.prototype.constructor=Graph;
graph = new Graph();
graph.align('left');
如果你不想从 Svg 继承,而是混合它,那么你仍然可以使用 prototype 来混合它的函数(并调用 Svg.apply 来获取所需的实例成员(:
function mixin(source, target){
for(thing in source){
if(source.hasOwnProperty(thing)){
target[thing]=source[thing];
}
}
};
function Svg() {
this.someInstanceValue=22;
}
Svg.prototype.align = function(value) {
if(value === 'left') return 0;
}
function Graph() {
// get Svg's instance properties
Svg.apply(this,arguments);
console.log('<text x="' + this.align('left') + '"></text>');
}
//mix in Svg:
mixin(Svg.prototype, Graph.prototype)
graph = new Graph();
graph.align('left');
- 可以“;超级“;可以在子类的方法内部使用,在不直接引用的情况下调用相应的超类方法
- 下划线扩展方法和结构.js子类
- 如何在 javascript 中从子类调用父方法
- ES6类:在超级方法中获取子类的正确名称
- Javascript继承:从超类的方法设置子类的属性
- Javascript子类对象不保留基类的属性/方法
- Javascript子类化,超级构造函数和使用自定义扩展会丢失基类方法
- 跨浏览器的方式来子类JavaScript Array并获取array[i]方法
- JavaScript:使用子类中的父方法
- RxJS 5子类Observable-静态方法返回父类的实例
- 如何调用父类'方法从JavaScript中的子类中删除,以便parent's的局部变量将是可访问的
- Javascript类扩展:从子类函数调用基类函数:似乎在调用静态方法
- 当子类继承并挂载父类时,父类是否运行生命周期方法?
- 在CoffeeScript中,调用基类方法和调用子类方法有什么区别?
- 在父类的方法中使用子类的类变量,这样变量就可以动态地改变(javascript)
- 是否有子类/继承/扩展javascript WebSocket的方法
- 如何在JavaScript内置JavaScript类的子类中使用基类方法
- 如何在JS/Seed的GObject子类中创建方法
- 如何在JavaScript中调用静态子类方法
- 仅slideToggle()子类的通用方法