sayAge:function(){console.log( this.age); return this;}
标签:
担任模式 担任成长史1、传统形式--原型链
过多的担任了没用的属性
2、借用结构函数
不能担任借用结构函数的原型
每次结构函数要多走一个函数
3、共享原型
不能添加本身原型的自界说属性,会把共享原型的东西的原型的属性也变动。
4、圣杯模式
//1.原型链 Grand.prototype.lastName = "li"; function Grand(){ } var grand = new Grand(); Father.prototype = grand; function Father(){ this.name = "kkk"; } var father = new Father(); Son.prototype = father; function Son(){ } var son = new Son(); //2、借用结构函数 function Person(name,age,sex){ this.name = name; this.age = age; this.sex= sex; } function Student(name,age,sex,grade){ Person.call(this,name,age,sex); this.grade = grade; } var student = new Student(); //3、共享原型 Father.prototype.lastName = "chen"; function Father(){ } function Son(){ } function inherit(target,origin){ target.prototype = origin.prototype; } inherit(Son,Father); var father = new Father(); var son = new Son(); //4、圣杯模式 !important Father.prototype.lastName = "chen"; function Father(){ } function Son(){ } function inherit(target,origin){ function F(){}; //target.prototype = new F();//不能放在F.prototype = origin.prototype之前,因为这里用的是改削之前的F的原型,没有指向origin的原型。 F.prototype = origin.prototype; target.prototype = new F();//target的原型是一个new的F东西,可以实现自界说的prototype的属性而不转变origin的原型。 target.prototype.constructor = target; target.prototype.uber = Origin.prototype;//超类,知道它的原型真正担任自哪里。 } inherit(Son,Father); var father = new Father(); var son = new Son(); 定名空间 //定名空间 -- 解决定名反复的问题 var grade = { class1:{ zhangsan:{ }, lisi:{ } }, class2:{ chen:{ }, ying:{ } } } var chen = grade.class2.chen;此刻解决定名反复的问题一般使用webpack打包工具
东西属性和要领的挪用1、持续挪用
//持续挪用 var methods = { name:"chen", age: 19, gender:"female", sayName:function (){ console.log(this.name); return this; }, sayAge:function(){ console.log(this.age); return this; }, sayGender:function(){ console.log(this.gender); return this; } }; methods.sayName().sayGender().sayAge();//chen female 19 //函数最后返回的都是this东西,可以实现持续挪用。2、通过属性名访谒东西属性
可实现通过参数挪用想要的东西属性,,从而减少代码量
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30803.html
- 上一篇:命令注入
- 下一篇: int z) {int f = fa[y]