function Person(){
this.createTime=new Date();
}
var instance=new Person();
function getInstance(){
return instance;
}
var instance
function getInstance(){
if(!instance){
instance=new Person();
}
return instance;
}
var getInstance(){
var instance;
return function(){
if(!instance){
instance=new Person();
}
return instance;
}
}();
function Person(){
//如果已经缓存了实例,则直接返回缓存的实例
if(typeof Person.instance==='object'){
return Person.instance;
}
this.createTime=new Date();
//缓存实例
Person.instance=this;
return this;
}
function Person(){
//缓存实例
var instance=this;
this.createTime=new Date();
//重写构造函数
Person=function(){
return instance;
}
}
//向原型添加属性 Person.prototype.prop1=true; var p1=new Person(); //在创建初始化对象后,再次向该原型添加属性 Person.prototype.prop2=true; var p2=new Person(); //开始测试 console.log(p1.prop1);//结果为true console.log(p2.prop1);//结果为true console.log(p1.prop2);//结果为undefined console.log(p2.prop2);//结果为undefined console.log(p1.constructor===Person);//结果为false console.log(p2.constructor===Person);//结果为false
function Person(){
//缓存实例
var instance=this;
//重写构造函数
Person=function(){
return instance;
}
//保留原型属性
Person.prototype=this;
//实例
instance=new Person();
//重置构造函数引用
instance.constructor=Person;
//其他初始化
instance.createTime=new Date();
return instance;
}
/* Singleton with Private Members, step 3. */
MyNamespace.Singleton = (function() {
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [1, 2, 3];
function privateMethod1() {
...
}
function privateMethod2(args) {
...
}
return { // Public members.
publicAttribute1: true,
publicAttribute2: 10,
publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
};
})();
/* General skeleton for a lazy loading singleton, step 1. */
MyNamespace.Singleton = (function() {
function constructor() { // All of the normal singleton code goes here.
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [1, 2, 3];
function privateMethod1() {
...
}
function privateMethod2(args) {
...
}
return { // Public members.
publicAttribute1: true,
publicAttribute2: 10,
publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
}
}
})();
/* General skeleton for a lazy loading singleton, step 2. */
MyNamespace.Singleton = (function() {
function constructor() { // All of the normal singleton code goes here.
...
}
return {
getInstance: function() {
// Control code goes here.
}
}
})();
/* General skeleton for a lazy loading singleton, step 3. */
MyNamespace.Singleton = (function() {
var uniqueInstance; // Private attribute that holds the single instance.
function constructor() { // All of the normal singleton code goes here.
...
}
return {
getInstance: function() {
if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.
uniqueInstance = constructor();
}
return uniqueInstance;
}
}
})();
/* SimpleXhrFactory singleton, step 1. */
var SimpleXhrFactory = (function() {
// The three branches.
var standard = {
createXhrObject: function() {
return new XMLHttpRequest();
}
};
var activeXNew = {
createXhrObject: function() {
return new ActiveXObject('Msxml2.XMLHTTP');
}
};
var activeXOld = {
createXhrObject: function() {
return new ActiveXObject('Microsoft.XMLHTTP');
}
};
})();
/* SimpleXhrFactory singleton, step 2. */
var SimpleXhrFactory = (function() {
// The three branches.
var standard = {
createXhrObject: function() {
return new XMLHttpRequest();
}
};
var activeXNew = {
createXhrObject: function() {
return new ActiveXObject('Msxml2.XMLHTTP');
}
};
var activeXOld = {
createXhrObject: function() {
return new ActiveXObject('Microsoft.XMLHTTP');
}
};
// To assign the branch, try each method; return whatever doesn't fail.
var testObject;
try {
testObject = standard.createXhrObject();
return standard; // Return this if no error was thrown.
}
catch(e) {
try {
testObject = activeXNew.createXhrObject();
return activeXNew; // Return this if no error was thrown.
}
catch(e) {
try {
testObject = activeXOld.createXhrObject();
return activeXOld; // Return this if no error was thrown.
}
catch(e) {
throw new Error('No XHR object found in this environment.');
}
}
}
})();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有