2013-06-21 49 views
0

我只是想通過不同的方式理解函數中定義的變量的行爲。所以我剛剛嘗試了一些代碼,並在下面作出評論。請告訴我是否理解錯誤。或者,如果我錯過了任何東西,請加上通過函數實例訪問JS函數變量

我想知道我怎麼能訪問像functionName.variableName通過功能情況(objTempFun1objTempFun2)函數聲明的變量就像我可以定義內部功能的方法,通過功能情況暴露閉包變量。

function tempFun() { 
    this.val1 = "this.var1"; 
    var val2 = "var var2"; //locally scoped var, not added to function instance nor to the prototype 

    this.getLocalVar = function() { 
     return val2; 
    }; 

    this.setLocalVar = function (arg) { 
     val2 = arg; 
    }; 
} 

tempFun.prototype.val3 = "fun.proto.var3"; 
tempFun.val4 = "fun.var4"; 

var objTempFun1 = new tempFun(); 
var objTempFun2 = new tempFun(); 

/*----------------------------------------------------------------------- 
     Variables defined as `this.variableName` 
     1. Are declared on prototype 
     2. Have separate values across instances 
     3. Cannot be accessed on function itself 
     -----------------------------------------------------------------------*/ 
document.write("<br />obj1-this.var1: " + objTempFun1.val1 + "<br />"); 
document.write("obj2-this.var1: " + objTempFun2.val1 + "<br />"); 
document.write("fun-this.var1: " + tempFun.val1 + "<br />"); 

objTempFun1.val1 = "this.var1x"; 
objTempFun2.val1 = "this.var1y"; 
document.write("obj1-this.var1: " + objTempFun1.val1 + "<br />"); 
document.write("obj2-this.var1: " + objTempFun2.val1 + "<br />"); 

/*----------------------------------------------------------------------- 
     Variables defined inside function as `var variableName` 
     1. Are closure-scoped; defined neither on function nor on prototype 
     2. Have separate values across function instances 
     3. Can only be accessed through function instance by adding methods on 
      a prototype (as above getLocalVar and setLocalVar methods) 
     -----------------------------------------------------------------------*/ 
document.write("obj1-var var2: " + objTempFun1.val2 + "<br />"); 
document.write("obj2-var var2: " + objTempFun2.val2 + "<br />"); 
document.write("fun-var var2: " + tempFun.val2 + "<br />"); 
document.write("obj1-printLocalVar: " + objTempFun1.getLocalVar() + "<br />"); 
document.write("obj2-printLocalVar: " + objTempFun2.getLocalVar() + "<br />"); 
objTempFun1.setLocalVar("var var2x"); 
objTempFun2.setLocalVar("var var2y"); 
document.write("obj1-printLocalVar: " + objTempFun1.getLocalVar() + "<br />"); 
document.write("obj2-printLocalVar: " + objTempFun2.getLocalVar() + "<br />"); 

/*----------------------------------------------------------------------- 
     Variables defined as `functionName.prototype.variablename` 
     1. Are declared on prototype 
     2. Have separate values across function instances 
     3. Cannot be accessed on function itself 
     -----------------------------------------------------------------------*/ 
document.write("obj1-this.proto.var3: " + objTempFun1.val3 + "<br />"); 
document.write("obj2-this.proto.var3: " + objTempFun2.val3 + "<br />"); 
document.write("fun-this.proto.var3: " + tempFun.val3 + "<br />"); 
objTempFun1.val3 = "fun.proto.var3x"; 
objTempFun2.val3 = "fun.proto.var3y"; 
document.write("obj1-this.proto.var3: " + objTempFun1.val3 + "<br />"); 
document.write("obj2-this.proto.var3: " + objTempFun2.val3 + "<br />"); 

/*------------------------------------------------------------------------ 
     Variables defined as `functionName.variablename` 
     1. Become member of function (or say 'F'unction instance, which is tempFun) 
      not the function instances (which are objTempFun1 & objTempFun2 above) 
     2. Cannot be accessed on function instances 
     ------------------------------------------------------------------------*/ 
document.write("obj1-fun.var4: " + objTempFun1.val4 + "<br />"); 
document.write("obj2-fun.var4: " + objTempFun2.val4 + "<br />"); 
document.write("fun-fun.var4: " + tempFun.val4 + "<br />"); 

尋找JSFiddle here

+1

_「變量定義爲'this.variableName' 1.在原型上聲明」_ - 不,它們在實例上聲明,但可以從原型方法訪問。如果它們是在原型上聲明的,那麼它們不會_「2。在實例中有單獨的值。」_ – nnnnnn

+0

第一個也可以說是最重要的規則是:(a)在函數內聲明的'val2'變量是'private'並且只能通過函數的特權方法從外部讀取/寫入,並且(b)對於要特權的方法,它必須是在外部函數中定義的函數,並且可以返回,作爲事件處理程序附加,或者定義在形式爲'this.foo = function(){...}'(其中cae外部函數必須作爲關鍵字爲「new」的構造函數調用)。外部定義的方法總是「公開」,但不是「特權」。 –

+0

@nnnnnn「但可以從原型方法訪問」,例如?我在protorype'val3'上聲明的變量在'objTempFun1'和'objTempFun2'之間有單獨的值 – Mahesha999

回答