如何在構造函數中使用公共公共函數訪問私有變量。如何在構造函數中使用通用公共函數訪問私有變量
function construct(){
var priValue1 = 0;
var priValue2 = 0;
var priValue3 = 0;
this.getvalue = function(_input){
return this[_input];
}
}
construct.prototype.init = function(){
if(this.getvalue("priValue1")){
console.log("Value 1")
}
}
var nc = new construct();
nc.init();
無法訪問私有變量。
'返回_input'取代'返回此[_Input]' –
http://jsfiddle.net/sandenay/0ac78v7d/ –
@ SandeepNayak我認爲這不會起作用。這隻會返回傳入的參數。我認爲他們想訪問他們聲明的變量。 –