English 中文(简体)
Java 模块模式和新关键词
原标题:Javascript Module Pattern and the new keyword

我获得单元模式的基础,并使用封闭设施,允许私人成员参加。

var Calculator = function() {
    var priv = 0;

    return {
        changePriv: function() { priv++;},
        printPriv: function() { console.log(priv);}
    }
}

var myCalc = Calculator();
myCalc.printPriv();
myCalc.changePriv();
myCalc.printPriv();

var myOtherCalc = Calculator();
myCalc.printPriv();

青春期产出

0
1
1

故意遗漏<代码>新 此处的关键词是,第一组电码(myCalc)至计算器物体。 它首先使用<代码>priv数值0,该数值已加减,然后打印出新的<代码>priv。 价值1。

But a) WHY does the next call to Calculator() end up returning a reference to the SAME object (as evidenced by the second 1 )? I know I can use new here and avoid that, but don t get why I have to. Isn t this function using object literal syntax to essentially create a new object and then return it? b) Since it does seem to be using the same function stack space (is that even the right way to think of it in JS?), why doesn t it zero out the priv variable in the process before returning the reference to the same object?

EDIT: Corrected sloppy/stupid mis (thanks scessor), DOES现在产生新的/单独计算器,即使没有使用new关键词。 由此可见a)和b。 我提出的问题是,“我是否使用<代码>新<>/代码>。 或者不援引模块主构人。 答案是,我猜想的是,它确实存在问题。 http://jsfiddle.net/MvMvy/5/> ......经营者的证明书仅以模块方式运作。

最佳回答

页: 1

myOtherCalc.printPriv();

然后是:

0
1
0
问题回答

你不需要在你的情况下使用<代码>新。

通常,如果你使用<条码>新,你就会看到你所说的建筑者。 在你的案件中,由于你人工返回了物体,它赢得了奖牌。 这样做是毫无意义的,并将在你把使用混为一谈后产生问题。 您可以“参加测试”您的物体,并将进入这一“会议”。

以及你在法典中打字:

var myCalc = Calculator();       //create calculator
myCalc.printPriv();              //"myCalc" private is 0
myCalc.changePriv();             //increment
myCalc.printPriv();              //"myCalc" private is 1

var myOtherCalc = Calculator();  //another calculator
myCalc.printPriv();              ///but you printed "myCalc" again

There s nothing to do with the "new" Operator... Here you get a well explained topic about proto/constructor : http://en.wikibooks.org/wiki/JavaScript/Access_Control

然而,这是一个非典型的例子,你可以这样做,因此,你只能通过食宿和固定方法获得食物:

function Calculator2() {
var priv = 0;
this.public = 0;
this.getPriv = function(){
    return  priv;
}
this.setPriv = function(val){
    priv = val;
}
}
Calculator2.prototype.changePriv = function(){
this.setPriv(this.getPriv()+1);
}
Calculator2.prototype.printPriv = function(){
    console.log("priv = " + this.getPriv());
}
Calculator2.prototype.changePublic = function(){
    this.public++;
}
Calculator2.prototype.printPublic = function(){
    console.log(this.public);
}

在这种情形下,总是可以通过一个接驳器和一套固定方法获得var车。

在下一个例子中,你有一家私人企业,即: 姓名:

<div id = "outputDiv" style="width:600px;height:400px;border:solid 1px #000"></div>
<script type="text/javascript">
    //<![CDATA[

//script : var SomeClass = function(className) {

    var __className__ = className;
    this.__className__ = "“公共违约等级”";
    var someString = new String("");

    this.setScopeText = function() { // void
        someString = "A new instance of "private [__classname__] : " +
        __className__ + """ +
        " has been created. Refering to [__className__]<br />" +
        "A new instance of " +
        this.__className__ +
        " has been created. Refering to [this.__className__]";
        return someString;
    };

    this.getScopeText= function (){
        return someString;
    }

    this.setOutput = function(elementId, someString){
        var outputPane = this.getSomePane(elementId);
        outputPane.innerHTML += "<p>" + someString + "</p>";
    }

    this.getSomePane = function(elementId){
        var outputP = document.getElementById(elementId);
        return outputP;
    }

}

SomeClass.prototype.changeClassNameVariable = function( str ){
    this.__className__  = str;
}

/最后声明。

/测试:

var sc = new SomeClass("foo");

sc.setOutput("outputDiv",sc.__className__);
sc.setOutput("outputDiv",sc.setScopeText());
sc.setOutput("outputDiv",sc.getSomePane("outputDiv"));

sc.__className__ = "“Some name”";
sc.setOutput("outputDiv",sc.__className__);
sc.setOutput("outputDiv",sc.setScopeText());

sc.changeClassNameVariable("酒吧");
sc.setOutput("outputDiv",sc.__className__);
sc.setOutput("outputDiv",sc.setScopeText());

/ end javascript and CDATA section

//]]>
</script>

Output in "div:outputDiv" :

“公共违约等级”

A new instance of "private [classname] : foo" has been created. Refering to [className] A new instance of “公共违约等级” has been created. Refering to [this.className]

[目标超文本部分]

“Some name”

A new instance of "private [classname] : foo" has been created. Refering to [className] A new instance of “Some name” has been created. Refering to [this.className]

酒吧

A new instance of "private [classname] : foo" has been created. Refering to [className] A new instance of 酒吧 has been created. Refering to [this.className]

-> className declared in constructor never changes! -> this.className or SomeClass.prototype.className is public and may be changed.

I hope this may help to understand the chain and my comment more clearly...





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

热门标签