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...