189 8069 5689

javascript单例,js class 单例

javascript代码,希望高手分析解释一下下面这段函数是什么意思?谢谢了!

看上去是用闭包实现了一个单例模式构造类。

西乡塘网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

SingletonInheritor 是一个包含 declare 方法的对象,

这个对象的declare() 方法就可以用来将你的class构造成单例。

var singleton1 = SingletonInheritor.declare(ClassA); // ClassA变成单例Class

var obj1 = ClassA.instance(); // 获取单例对象

var obj2 = ClassA.instance(); // 获取到的对象与上面是同一个

JavaScript类的几种写法

2. 构造函数(public, private属性和方法) 1: function Person(iName, iAge){ 2: //private field 3: var name = iName; 4: var age = iAge; 5: 6: //private method 7: var privatefn = function(){ 8: alert(name); 9: } 10: 11: return { 12: //public field 13: Name: "hello " + name, 14: Age: "hello " + age, 15: 16: ShowStudent: function(){ 17: privatefn(); 18: alert(this.Name); 19: } 20: }; 21: }调用:(new Person("xiao","10")).ShowStudent(); 3. 原型方法(prototype) 1: function c(){} 2: c.prototype={ 3: name: "init value a", 4: setName: function(iName){ 5: this.name=iName; 6: }, 7: getName: function(){ 8: alert('hello from c, name: ' + this.name); 9: } 10: }; 11: (new c).getName(); // 输出hello from c, name: init value a 4. 构造函数+原型方法(prototype) 1: function Person(iName) { 2: this.name = iName; 3: }; 4: 5: Person.prototype={ 6: getName: function(){ 7: returnthis.name; 8: } 9: }; 10: 11: //调用 12: var b = new Person("jack"); 13: alert(b.getName()); 5. 构造函数+原型方法(prototype)- 节省内存的写法 1: function Person(iName, iAge){ 2: this.name=iName; 3: this.age=iAge; 4: 5: //对象实例都共享同一份方法不造成内存浪费 6: if(typeof Person._initialized == "undefined"){ 7: Person.prototype.ShowStudent=function(){ 8: alert(this.name); 9: }; 10: Person._initialized=true; 11: } 12: } 13: //调用 14: (new Person("jack","20")).ShowStudent();以上的实现方法如果不用_initialized的方法,也可以指向一个外部函数,道理一样。 6. JavaScript类的单例(Singleton)模式写法 1: var MyNamespace = {}; 2: MyNamespace.Singleton = (function() { 3: var uniqueInstance; // Private attribute that holds the single instance. 4: function constructor() { // All of the normal singleton code goes here. 5: // Private members. 6: var privateAttribute1 = false; 7: var privateAttribute2 = [1, 2, 3]; 8: function privateMethod1() { 9: //... 10: } 11: function privateMethod2(args) { 12: //... 13: } 14: return { // Public members. 15: publicAttribute1: true, 16: publicAttribute2: 10, 17: publicMethod1: function() { 18: // ... 19: }, 20: publicMethod2: function(args) { 21: // ... 22: } 23: } 24: } 25: return { 26: getInstance: function() { 27: if(!uniqueInstance) { // Instantiate only if the instance doesn't exist. 28: uniqueInstance = constructor(); 29: } 30: return uniqueInstance; 31: } 32: } 33: })(); 34: 35: //调用: 36: MyNamespace.Singleton.getInstance().publicMethod1(); JavaScript好书推荐(只推3本,须精读)

在javascript中使用单例模式,出现问题。

好像 似乎 大概 是这个样子滴 匿名函数问题:

把 window["lib"]["lognModule"] =(function(){code})();

改成window["lib"]["lognModule"] =(function(){code});

不要最后那个括号,最后那个括号表示执行该返回的函数;

你只是让window.lib.lognModule对该函数的引用,而不是引用执行完了的结果。

javascript中如何实现单例模式

(function () {                //自运行函数,初始化一些必要内容(也可叫封包)

var obj = null;           //定义一个变量,保存实例对象

window.getObject = function() {    //初始化外部接口好调用这个实例

if (obj === null) {            //如果实例为空就new一个实例并保存到obj

obj = new (function () {

var rows = 0;

this.getRows = function () {

return rows;

}

this.setRows = function (value) {

rows = value;

}

})();

}

return obj;         //最后返回该实例

}

})();

调用如下:

getObject()

得到的对象有getRows和setRows两个函数

可以先调用setRows更改该属性值来判读多次调用getObject得到的是不是同一对象


新闻名称:javascript单例,js class 单例
本文URL:http://cdxtjz.cn/article/dsejdgd.html

其他资讯