Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中从超类扩展方法_Javascript_Oop_Object_Inheritance_Methods - Fatal编程技术网

如何在javascript中从超类扩展方法

如何在javascript中从超类扩展方法,javascript,oop,object,inheritance,methods,Javascript,Oop,Object,Inheritance,Methods,我有一个基类中的方法,我想保留在子类中,但只是添加到它。我发现了很多关于用属性和方法扩充类和对象的东西,但是我找不到,或者不理解,如何仅仅扩充方法。最坏的情况是,我必须将父类的整个方法粘贴到子类中,但这似乎是重复的代码。。。请帮忙 function someObject (){ this.someProperty = 1; this.incrementProperty = function incrementProperty(){ this.property

我有一个基类中的方法,我想保留在子类中,但只是添加到它。我发现了很多关于用属性和方法扩充类和对象的东西,但是我找不到,或者不理解,如何仅仅扩充方法。最坏的情况是,我必须将父类的整个方法粘贴到子类中,但这似乎是重复的代码。。。请帮忙

function someObject (){
    this.someProperty = 1;
    this.incrementProperty = function incrementProperty(){   
        this.propertyOfSomeObject += 1;
    }
}

function newObject (){
    someObject.call(this);
    this.incrementProperty =  function incrementProperty(){
        //do everything the super class has for this property already
        return this.someProperty;
    }
}

var incrementer = new newObject;
alert (incrementer.incrementProperty()); //I want output to be 2

这应该可以做到,您必须使用prototype来使用javascript实现真正的oo概念

function someObject (){
    this.someProperty = 1;
    this.propertyOfSomeObject = 0;
    this.incrementProperty = function incrementProperty(){   
        this.propertyOfSomeObject += 1;
        return this.propertyOfSomeObject;
    }
}

function newObject (){
    someObject.call(this);
    this.incrementProperty =  function incrementProperty(){
        this.__super__.incrementProperty.apply(this);
        return this.propertyOfSomeObject + 1;
    }
}

newObject.prototype = new someObject()
newObject.prototype.__super__ = newObject.prototype

var incrementer = new newObject();
alert(incrementer.incrementProperty()); //I want output to be 2
尝试从newObject中删除incrementProperty,它将返回1

// parent object
function someObject () {
    this.someProperty = 1;
}

// add incrementProperty to the prototype so you're not creating a new function
// every time you instantiate the object
someObject.prototype.incrementProperty = function() {   
  this.someProperty += 1;
  return this.someProperty;
}

// child object
function newObject () {
    // we could do useful work here
}

// setup new object as a child class of someObject
newObject.prototype = new someObject();
// this allows us to use "parent" to call someObject's functions
newObject.prototype.parent = someObject.prototype;
// make sure the constructor points to the right place (not someObject)
newObject.constructor = newObject;

newObject.prototype.incrementProperty = function() {
    // do everything the super class has for this property already
    this.parent.incrementProperty.call(this);
    return this.someProperty;
}

var incrementer = new newObject();
alert (incrementer.incrementProperty()); // I want output to be 2
请参阅:

发件人:

覆盖功能

有时
子项
需要扩展
父项
函数

您希望“child”(=rusionmini)做一些额外的事情。当RussionMini可以调用仓鼠代码来做一些事情,然后再做一些额外的事情时,您不需要将仓鼠代码复制并粘贴到RussionMini

在下面的例子中,我们假设一只仓鼠可以每小时跑3公里,而一只Russion mini只能跑一半的速度。我们可以在RussionMini中硬编码3/2,但如果这个值要更改,我们在代码中有多个地方需要更改。下面是我们如何使用仓鼠.prototype来获得父(仓鼠)的速度

我通常使用这个库用JavaScript编写类。这就是我将如何使用
augment
重写您的代码:

var Foo = Object.augment(function () {
    this.constructor = function () {
        this.someProperty = 1;
    };

    this.incrementProperty = function () {
        this.someProperty++;
    };
});

var Bar = Foo.augment(function (base) {
    this.constructor = function () {
        base.constructor.call(this);
    };

    this.incrementProperty = function () {
        base.incrementProperty.call(this);
        return this.someProperty;
    };
});
正如您所看到的,由于
Bar
扩展了
Foo
,它将
Foo.prototype
作为一个参数(我们称之为
base
)。这允许您轻松调用基类
构造函数
递增属性
函数。它还表明
构造函数本身只是原型上定义的另一种方法

var bar = new Bar;

alert(bar.incrementProperty());

输出将如预期的那样为
2
。请亲自查看演示:

Your
someObject.call(this)
没有用,因为您正在用下一行的另一个方法覆盖
someObject()调用中分配的
incrementProperty
方法。所以没有这样的“超级”函数可以调用。它已经被删除了。所以如果我写var inc2=newsomeobject;警报(inc2.incrementProperty());它不会运行此。propertyOfSomeObject+=1?当然会,但那是因为您直接从
someObject()
获取对象。你的问题不是这样的。在您的问题中,您将获得
newObject()
,然后将
someObject()
函数应用于同一对象。因此,首先对象从
someObject()
中获取
incrementProperty
,然后该属性被
newObject()
中的属性覆盖。不,这与JavaScript完全不同。他们使用构造函数是因为他们希望它像发明它时的Java一样,但其基本概念完全不同。基本上,原型继承是对象的查找链,构造函数用于建立两个对象之间的关系。因此,您的
这个
对象实际上是从位于
newObject.prototype的对象继承的。因此,您添加到
newObject.prototype
的任何方法都可以从您创建的对象访问。我建议看一两本教程。我支持@BlueSkies。在继续之前,您可能应该先学习js原型的基础知识,否则您将完全不知所措。特别是如果您来自Java背景。语法可能会产生误导,这不是为原型分配方法的方式。您只需将名为prototype的属性直接放在对象上。我的意思是这有点有效,但它与原型遗传无关。您可以使用
foo
,它将是相同的。您的意思是
.prototype
的行为与
.foo
相同吗?正确。JavaScript中有关OO的
.prototype
属性位于创建对象的函数上。不在对象本身上。因此
someObject()
newObject()
都有一个
.prototype
属性,可以为其分配方法。这些方法然后由从这些函数创建的对象继承。我认为正确的方法是newObject.prototype.incrementProperty=function(){this.someProperty+=1;}。。。但即使我这样做了,在你的例子中,代码在两个类中都是重复的,所以它没有重用代码,这就是我要做的。我不特别喜欢这个例子,因为它让人困惑。首先,它将函数存储为对象的一个属性(这使它看起来好像不是真正共享的),而基对象被设置为原型(这也有点令人困惑,因为基类原型上实际上没有任何内容,因此没有任何内容可“继承”)。非常感谢你!我特别喜欢JSFIDLE链接——非常好的添加。我确实有几个后续问题。1-newObject.prototype.parent定义是指向someObject类的指针,对吗?因此,如果在newObject继承之后someObject类被动态更改,newObject类不会更改,但是对newObject.prototype.parent的引用会更改吗?另外,我删除了显式的newObject.constructor定义,并四处翻看,它似乎指向了正确的构造函数-这一行真的有必要吗?我使用这种继承方法最终遇到了这个问题:
var bar = new Bar;

alert(bar.incrementProperty());