//原型的缺点 function Box(){} Box.prototype={ 'name':'gao', age:23, family:['哥哥','姐姐','妹妹'], run:function(){ return this.name+'--'+this.age; } } //1.缺少构造函数来初始化参数 var box1=new Box(); var box2=new Box(); box1.age=12; //2.原型中存在引用类型,多个实例会共享.例如1个追加数组元素,其他实例同时被追加 box1.family.push('弟弟'); console.log(box1.family); console.log(box2.family); console.log(box2.age); //3.解决以上2个问题 //不需要共享的使用构造方法 function BBox(name,age){ this.name=name; this.age=age; this.family=['1','2','3']; } //需要共享的地方使用原型 BBox.prototype={ constructor:BBox, run:function(){ return this.name+'--'+this.age+'--'+this.family; } } var bbox1=new BBox('gao',23); console.log(bbox1.run()); //4.由于在原型中,不管你是否调用了共享方法,它都会初始化一遍,于是我们可以简化如下.简称动态原型模式 function BBBox(name,age){ this.name=name; this.age=age; this.family=['1','2','3']; //原型只需要执行1次,立即共享 if(typeof this.run !='function'){ BBBox.prototype.run=function(){ return this.name+'--'+this.age+'--'+this.family; } } } var BBBox1=new BBBox('chen',24); console.log(BBBox1.run());
代码1:<!--代码开始--> <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script>...
维护老项目中客户提到一个页面中有6个表单以上,导致每次保存一个其他的数据全部丢失,自己比较懒没有全部更换为ajax.用户每次输入完成或者选择完成记录cookie,每次提交后加载页面完成初始化cookie即可。以下代码取自w3school比较完善,之前在其他博客使用的经常出现bug,这个比较推荐使用:...
jQuery 插件——jqueryrotate,它可以实现旋转效果。jqueryrotate 支持所有主流浏览器,包括 IE6。如果提示方法不存在,可能是你的Jquery版本过低或者过高。基本语法:$('#img').rotate(90);//旋转90度其他的参数:参数类型说明默认值...
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>点击复制</title> </head>...
var的作用域是全局的,let的作用域是块级的,直接看代码(其他的小区别忽略):<script type="text/javascript"> for (var a = ...