在JavaScript中的另一个内部调用一个原型方法 [英] Calling one prototype method inside another in javascript

查看:59
本文介绍了在JavaScript中的另一个内部调用一个原型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var Ob = function(){


}

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

}

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

}

window.onload = function(){
var o = new Ob();
o.add();
}

我想调用这样的东西,我该怎么称呼,当然我将 inc 作为内部函数添加到 add 中,我可以这样做但没有内部函数.我该怎么办?

I would like to call something like this,how can i call, ofcourse i put inc as inner function to add I can do that but without having the inner function. how do i do that ?

推荐答案

很简单:

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

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

创建Ob的实例时,会将原型中的属性复制到该对象.如果要从实例的另一个方法中访问实例的方法,则可以使用this.

When you create the instance of Ob properties from prototype are copied to the object. If you want to access the methods of instance from within its another method you could use this.

这篇关于在JavaScript中的另一个内部调用一个原型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆