标签:
闭包:
var singleton = function( fn ){
var result;
return function(){
return result || ( result = fn .apply( this, arguments ) );
}
}
//test
function aa(){}
var a = aa()
var b = aa()
a===b
构造函数内部判断
function Construct(){
// 确保只有单例
if( Construct.unique !== undefined ){
return Construct.unique;
}
// 其他代码
this.name = "Construct";
Construct.unique = this;
}
//test
var t1 = new Construct() ;
var t2 = new Construct() ;
t1 === t2
标签:
原文地址:http://www.cnblogs.com/shenggen/p/5679785.html