87,901
社区成员
发帖
与我相关
我的任务
分享
(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())
function test(){
}
test.prototype = (function(){
var data;
return {
get: function(){
return data;
},
set: function(val){
data = val;
return this;
}
}
})();
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();
(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都可以。 function Test(){
this._private;
}
Test.prototype = {
get: function(){
return this._private;
},
set: function(val){
this._private = val;
return this;
}
}
(function(){
function test(){
var data;
this.get = function() {
return data;
}
this.set = function( val ) {
data = val;
return this;
}
}
window.test = test;
})();
(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())
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