解决方案 »

  1.   

    function test(){

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

  2.   

    这么不行
    [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();
      

  3.   

    你叫他访问,他能不访问吗?java,php等属性的私有变量,难道不能通过公用方法来访问吗?
      

  4.   

    不是一样吗
    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
      

  5.   

    (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())
      

  6.   

    用prototype太麻烦了, 难以模拟private访问修饰
    这样方便 (function(){
        function test(){
            var data;

    this.get = function() {
    return data;
    }

    this.set = function( val ) {
    data = val;
    return this;
    }
        }
        window.test = test;
    })();
      

  7.   

    用prototype 是无法实现私有的。
    私有的话只有#8的方法。
      

  8.   

    对,是实现不了。
    不过#8那样写就跟class不像。
    看来,还只能大家默认一下。
    function Test(){
    this._private;
    }
    Test.prototype = {
            get: function(){
                return this._private;
            },
            set: function(val){
                this._private = val;
                return this;
            }
    }
      

  9.   

    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都可以。
      

  10.   

    来点高级。
    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();
      

  11.   

     
    this.id = ++arguments.callee.prototype.uid;
    改成 
    this.id = arguments.callee.prototype.uid++;
    可能好一点