闭包:
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
时间: 2024-10-24 00:41:35