- 只能在全局實例化一次的對象
/**
* 全局變量,其實就是單例
*/
window.count = 0;
function index()
{
count +=1
function content()
{
count +=1
}
}
/**
* 標準實現(xiàn)單例
*/
function Singleton()
{
this.count = 0;
if(Singleton.interface !== undefined)
{
return Singleton.interface;
}
//靜態(tài)屬性
Singleton.interface = this;
}
var s1 = new Singleton();
var s2 = new Singleton();
console.log(s1 == s2) //輸出:true