(function() {
function User() {
//some properties
}
//private fn 1
User.prototype._aPrivateFn = function() {
//private function defined just like a public function,
//for convetion underscore character is added
}
//private function type 2
//a closure
function _anotherPrivateFunction() {
// do something
}
//public function
User.prototype.APublicFunction = function() {
//call private fn1
this._aPrivateFn();
//call private fn2
_anotherPrivateFunction();
}
window.UserX = User;
})();
//定義javascript對象的私有方法的兩種方法中哪一種更好,尤其是內存管理和性能方面。JavaScript對象的 - 私有方法:哪種方式更好
極有可能依賴於瀏覽器。 – 2011-01-10 08:18:22
可悲的是,使用第二種方法需要在範圍鏈中多走一步,因此(可能不明顯)較慢。但你有更多的控制權。 – 2011-01-10 10:34:43