第一种
var a = {};
uniqueId = (function(){
var id = 0;
return function(){
return id++;
}
})()
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
第二种
var a = {};
a.uniqueId = function(){
var id = 0;
return function(){
id++;
return id
}();
}
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
闭包的差异能够说清楚么?
var a = {};
uniqueId = (function(){
var id = 0;
return function(){
return id++;
}
})()
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
第二种
var a = {};
a.uniqueId = function(){
var id = 0;
return function(){
id++;
return id
}();
}
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
alert(a.uniqueId());
闭包的差异能够说清楚么?