//原型的缺点
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()); 由于工作需要长期使用Ajax,一个页面重复的AJAX请求太多,于是封装起来,只需要编写回调函数/* ------------- 使用方法: 1.ajaxrequest()函数执行准备的参数(1.请求地址2.发送数据字符串拼接3.type值可选get/post4.回调函数名称) exam...
window.setInterval(method,time)方法本身会返回一个资源句柄,使用clearInterval(Intervalid)方法即可清除定时器<script> var num=0; //每隔1秒再控制台输...
TmodJS是一套完整的前端模块框架。 虽然我们PHP框架自带各种模板引擎,但是始终是后端模板引擎。例如我们在使用ThinkPHP3.2.3中如果是Ajax异步加载页面,拼接HTML真的是很头疼的一件事情。...
<!-- 三个核心方法 openDatabase:这个方法使用现有数据库或创建...
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body>...
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body>...