English 中文(简体)
公共变量
原标题:javascript private function access public variable

我有这个类:

function ctest() {
    this.var1 = "haha";
    this.func1 = function() {
        alert(this.var1);
        func2();
        alert(this.var1);
    }
    var func2 = function() {
        this.var1 = "huhu";
    }
}

并称之为:

    var myobj = new ctest();
    myobj.func1();

是否应该第二次警报会弹出“huhu”? func2是私人的,

如果私人功能无法访问公共变量, 我该怎么做?

提前感谢!

最佳回答

您需要为调用 func2 的调用提供一个上下文 :

this.func1 = function() {
    alert(this.var1);
    func2.call(this);
    alert(this.var1);
}

如果没有上下文, 调用将使用全局对象( 即 < code> window ) - 您应该看到当您运行当前代码时, < code> window. var1 会在两个警告之间创建 。

问题回答

函数不与实例挂钩,因此您援引 func2 的语句最终成为引用,而没有 this 指向预期实例。

您可以修改引用以包括上下文:

function ctest() {
    this.var1 = "haha";
    this.func1 = function() {
        alert(this.var1);
        func2.call(this);
        alert(this.var1);
    }
    var func2 = function() {
        this.var1 = "huhu";
    }
}

或者您可以保留一个变量, 并围绕 :

function ctest() {
    var that = this;
    this.var1 = "haha";
    this.func1 = function() {
        alert(this.var1);
        func2();
        alert(this.var1);
    }
    var func2 = function() {
        that.var1 = "huhu";
    }
}

在联署材料中没有私密的东西, 但是您可以使用 < a href=> "http://jibbering.com/ faq/ notes/ clocuresss/" rel="nofollow" > clocures 来玩屏幕游戏。

例如,在您的例子中,您不需要将 var1 公有财产暴露为公共财产。您可以很容易地将您的代码重写如下:

function ctest() {
    var var1 = "haha";

    this.func1 = function() {
        alert(var1);
        func2();
        alert(var1);
    }

    var func2 = function() {
        var1 = "huhu";
    }
}

由于 func1 func2 都具有相同的范围, 它们被定义在同一函数中, chest - 他们可以访问相同的变量。 当然, 如果您没有 var1 , 那么: myobj. var1 将是 un defed

如果您希望 var1 作为属性被曝光, 那么您所需要的是 < a href=" https:// developmenter.mozilla.org/ en/ JavaScript/ Reference/ Global_ Objects/Function/bind" rel= “ nofollow" >bind func2 到您在构建器中创建的对象实例 :

function ctest() {
    this.var1 = "haha";
    this.func1 = function() {
        alert(this.var1);
        func2();
        alert(this.var1);
    }
    var func2 = function() {
        this.var1 = "huhu";
    }.bind(this);
}

否则 func2 将有一个不同的上下文对象( this )。 如果浏览器不支持 >>bind ,而您不想使用shim(如上文链接所示),您可以再次利用关闭:

function ctest() {
    this.var1 = "haha";
    this.func1 = function() {
        alert(this.var1);
        func2();
        alert(this.var1);
    }
    var context = this;
    var func2 = function() {
        context.var1 = "huhu";
    }
}

IMVHO不那么优雅。

function ctest() {
    this.var1 = "haha";
    this.func1 = function() {
        alert(this.var1);
        func2(this);
        alert(this.var1);
    }
    var func2 = function(obj) {
        obj.var1 = "huhu";
    }
}

以其他方式解决问题,





相关问题
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.

热门标签