怎么实现JS类私有属性?

ywq0127 2014-03-19 11:31:53
上代码:

(function(){
function test(){
var data;
}
test.prototype = {
get: function(){
return data;
},
set: function(val){
data = val;
return this;
}
}
window.test = test;
})();


var obj1 = new test();
obj1.set("1234");
console.log(obj1.get());

var obj2 = new test();
console.log(obj2.get())

现在的问题是,两个对象都能访问到data的值。而不是像java,php等属性是绑定在对象上的,现在这里的这种情况就有点像data是类静态属性或常量。
...全文
581 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
你叫他访问,他能不访问吗? java,php等属性的私有变量,难道不能通过公用方法来访问吗?
ywq0127 2014-03-19
  • 打赏
  • 举报
回复
引用 3 楼 u011461314 的回复:
这么不行 [code=javascript][/code function Test(){ } Test.prototype = (function(){ var data; return { get: function(){ return data; }, set: function(val){ data = val; return this; } } })(); var $a = new Test(); $a.set('wawa'); var $v = $a.get();
哥们有没有看清需求啊?如果单纯的只是为了不能使用“.”访问的话,我一开始的就可以了。
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
这么不行 [code=javascript][/code function Test(){ } Test.prototype = (function(){ var data; return { get: function(){ return data; }, set: function(val){ data = val; return this; } } })(); var $a = new Test(); $a.set('wawa'); var $v = $a.get();
ywq0127 2014-03-19
  • 打赏
  • 举报
回复
引用 1 楼 u011461314 的回复:
function test(){
			
		}	
	test.prototype = (function(){
		var data;
		return {
	        get: function(){
	            return data;
	        },
	        set: function(val){
	            data = val;
	            return this;
	        }
	    }
	})();
不行的,这种我也测试过了。
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
function test(){
			
		}	
	test.prototype = (function(){
		var data;
		return {
	        get: function(){
	            return data;
	        },
	        set: function(val){
	            data = val;
	            return this;
	        }
	    }
	})();
ywq0127 2014-03-19
  • 打赏
  • 举报
回复
引用 14 楼 u011461314 的回复:
来点高级。
function Test() {			
			this.id = ++arguments.callee.prototype.uid;						
		}
		Test.prototype = (function() {
			var data = [];
			return {
				get : function() {
					return data[this.id];
				},
				set : function(val) {
					data[this.id] = val;
					return this;
				}
			}
		})()
		Test.prototype.uid=0;

		var $a = new Test();
		$a.set('wawa');
		var $v = $a.get();
		var $b = new Test();
		$b.set('xxx');
		var $b = $b.get();
this.id = ++arguments.callee.prototype.uid; 改成 this.id = arguments.callee.prototype.uid++; 可能好一点
ywq0127 2014-03-19
  • 打赏
  • 举报
回复
引用 13 楼 l676331991 的回复:
LZ的代码有问题好不好,demo成功了只是因为先调用了set再调用get而已! 这也是两个实例共用了一个data的原因啊,像静态的一样是吧,因为这个data被set到window上去了!!

(function(){
    function test(){
        var data;  //这个data形同虚设,下面的get/set根本不是用的这货呀!
    }
    test.prototype = {
        get: function(){
            return data;
        },
        set: function(val){
            data = val;  //这个data根本不能引用test内部的data,其实这里是window.data = val;
            return this;
        }
    }
    window.test = test;
})();
要实现你的需求,#1、#7、#8都可以。
多谢指出
ywq0127 2014-03-19
  • 打赏
  • 举报
回复
感谢上面各位热心帮忙,JS基础不扎实 看来想实现属性不共用,只能是#7 #8这种写法了
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
来点高级。
function Test() {			
			this.id = ++arguments.callee.prototype.uid;						
		}
		Test.prototype = (function() {
			var data = [];
			return {
				get : function() {
					return data[this.id];
				},
				set : function(val) {
					data[this.id] = val;
					return this;
				}
			}
		})()
		Test.prototype.uid=0;

		var $a = new Test();
		$a.set('wawa');
		var $v = $a.get();
		var $b = new Test();
		$b.set('xxx');
		var $b = $b.get();
l676331991 2014-03-19
  • 打赏
  • 举报
回复
LZ的代码有问题好不好,demo成功了只是因为先调用了set再调用get而已! 这也是两个实例共用了一个data的原因啊,像静态的一样是吧,因为这个data被set到window上去了!!

(function(){
    function test(){
        var data;  //这个data形同虚设,下面的get/set根本不是用的这货呀!
    }
    test.prototype = {
        get: function(){
            return data;
        },
        set: function(val){
            data = val;  //这个data根本不能引用test内部的data,其实这里是window.data = val;
            return this;
        }
    }
    window.test = test;
})();
要实现你的需求,#1、#7、#8都可以。
xuzuning 2014-03-19
  • 打赏
  • 举报
回复
私有属性 是指不能在对象外直接访问的属性 私有属性 不具有继承性
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
我刚才没想到,他们是共用的。
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
对,是实现不了。 不过#8那样写就跟class不像。 看来,还只能大家默认一下。
		function Test(){
			this._private;			
		}				
	Test.prototype = {
	        get: function(){
	            return this._private;
	        },
	        set: function(val){
	            this._private = val;
	            return this;
	        }
	}
天际的海浪 2014-03-19
  • 打赏
  • 举报
回复
用prototype 是无法实现私有的。 私有的话只有#8的方法。
挨踢直男 2014-03-19
  • 打赏
  • 举报
回复
用prototype太麻烦了, 难以模拟private访问修饰 这样方便

(function(){
    function test(){
        var data;
		
		this.get = function() {
			return data;
		}
		
		this.set = function( val ) {
			data = val;
			return this;
		}
    }
    window.test = test;
})();
王集鹄 2014-03-19
  • 打赏
  • 举报
回复
(function(){
    function test(){
        var data;
        var self = this;
        this.get = function() {
            return data;
        }
        this.set = function(val) {
            data = val;
            return self;
        }
    }
    window.test = test;
})();

var obj1 = new test();
obj1.set("1234");
console.log(obj1.get());
 
var obj2 = new test();
console.log(obj2.get())
zhjdg 2014-03-19
  • 打赏
  • 举报
回复
不是一样吗
class Test{
	private $data;
	public function get(){
		return $this->data;
	}
	public function set($val){
		$this->data = $val;
	}
}

$a = new Test();
$a->set('wawa');
$val = $a->get();
指的是不能$a->data; 同理javascript中 不能$a.data

87,901

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧