js学习(20160519)
2016-05-19 09:30
519 查看
js高级学习 点击打开链接
1、面向对象
1、面向对象
<script> (function(){ //单例模式 swlSingleTon = function(className, dict){ var funcStr = []; if(dict){ for(var k in dict){ var tmp = dict[k]; if(typeof tmp === "string"){ tmp = "\"" + tmp + "\"";//字符串 } funcStr.push(" this." + k + " = " + tmp + " ;"); } } var str = []; str.push(" " + className + " = (function(){ "); str.push(" function " + className + "(){ " + funcStr.join("") + " } "); str.push(" var _instance; "); str.push(" function Instance(){ "); str.push(" if(!_instance){ "); str.push(" _instance = new " + className + "(); "); str.push(" }"); str.push(" return _instance; "); str.push(" } "); str.push(" return { "); str.push(" Instance : Instance "); str.push(" } "); str.push(" })(); "); // console.log(str.join("")); eval(str.join("")); } Person = (function(){ function Person(){ this.tall = function(){ return "person tall"; } } // 单例模式 var _instance; function Instance(){ if(!_instance){ // alert(1); _instance = new Person(); } return _instance; } return { Instance : Instance } })(); swlSingleTon("Man", { name: "LH", age: 25, hair: function(){ return "man hair"; } }); swlSingleTon("Woman", { name: "LH", age: 25, hair: function(){ return "woman hair"; } }); })(); alert(Person.Instance().tall()); alert(Man.Instance().hair()); alert(Woman.Instance().hair()); </script>
相关文章推荐
- Json的解析
- js获取显示器分辨率
- js复选框变单选功能
- js中的运算符操作顺序
- javascript中对Date类型的常用操作小结
- JS Attribute属性操作详解
- javascript闭包学习笔记
- javascript实现打印html制定区域同时消除header/footer区域
- JSP+JavaScript实现图片切换
- C#实现百度地图附近搜索&调用JavaScript函数
- ES6新特性:Javascript中Generator(生成器)
- JSP文件引用其他文件
- JavaScript内存泄漏
- javascript执行顺序总结
- javascript 监听事件
- js数组去重的4个方法
- 函数表达式
- JavaScript中的匿名函数及函数的闭包
- javascript执行顺序(三)
- javascript执行顺序(二)