在javascript中调用另一个原型方法

ind*_*vil 52 javascript oop prototype

var Ob = function(){


}

Ob.prototype.add = function(){
    inc()

}

Ob.prototype.inc = function(){
    alert(' Inc called ');

}

window.onload = function(){
var o = new Ob();
o.add();
}
Run Code Online (Sandbox Code Playgroud)

我想打电话给这样的话,我怎么称呼,当然我把inc作为内部函数来添加我可以做到但是没有内部函数.我怎么做 ?

bjo*_*rnd 78

这很简单:

Ob.prototype.add = function(){
    this.inc()
}

Ob.prototype.inc = function(){
    alert(' Inc called ');
}
Run Code Online (Sandbox Code Playgroud)

当您Ob从prototype 创建属性的实例时,将其复制到该对象.如果要从其他方法中访问实例的方法,可以使用this.