日期:2014-05-16 浏览次数:20391 次
(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(){
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())
(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; //这个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;
})();