English 中文(简体)
利用公共方法使用私人方法?
原标题:Call private method from public method?
  • 时间:2011-06-21 05:40:57
  •  标签:
  • javascript

I´ve defined a class like this:

function Class1(){
    this.Func1 = function(){
       /* Methods and vars */
    };

    function Func2(){
       /* Methods and vars */
    };

};

我想找到一种办法,将公用方法(或获得公有变量的价值)从私人方法(Func2(12))上调。 任何 su择?

Pd:如果我使用的术语主要针对物体,则Sorry,因为我是C++的编程者,I mkinda是javascript式节目中的新版。

最佳回答

可在<代码>Func1上直接打电话Func2:

this.Func1 = function() {
  Func2();
};

但是,从<条码>Func1到。 由于<代码>Func2(可能的话)在名称时对<代码>有不同的范围和不同定义;<代码>将不予界定。 如下表所示,如果从内部职能中使用,你可以使用另一个变量来保持其价值,从而节省范围。 还可在地方范围内避免提及<代码>Func1:

var Func1 = this.Func1 = function() {
  // fun stuff
};

function Func2() {
  Func1();
}

之所以这样做,是因为它并不依赖不断变化的参考资料this

问题回答

关闭

function Class1(){
        this.Func1 = function(){
           /* Methods and vars */
        };

        var me = this;

        function Func2(){
           me.Func1();
        };

    };

公开方法在同样范围内公布,因此,你只能说:

function Object1(){
    this.publicMethod = function(){         
        alert("I m a public method ;)");
    };
    var that = this;
    function privateMethod(){
        return that.publicMethod.apply(that,arguments);
    };
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签