调用私有方法

时间:2015-04-29 12:09:24

标签: javascript

我跟随此blog并玩代码。

T data = (T)(Integer)0;

我怎么可以打电话给function MyClass() { var self = this; var privateVar = 'My Private Variable'; this.publicVar = 'My Public Variable'; var privateFunction = function () { self.publicVar += ' Modified By A Private Fucntion'; alert(self.publicVar); }; privateFunction(); //1: why this code not working. kindly give the reason for this. } ? 我正在寻找的输出//"我的私人变量由私人功能修改"

1 个答案:

答案 0 :(得分:0)

此代码应该有效。很可能你不是在任何地方实例化MyClass。

function MyClass() {
var self = this;
var privateVar = 'My Private Variable';
this.publicVar = 'My Public Variable';
var privateFunction = function () {
  self.publicVar += ' Modified By A Private Fucntion';
  alert(self.publicVar);
};
privateFunction(); // why this code not working..
};
new MyClass(); //Run MyClass, triggering all code.