继承模式,命名空间优化管理

继承模式,命名空间优化管理

继承的发展史

传统形式—>原型链

function Grand(){}Grand.prototype.lastName="ji";var grand=new Grand();Father.prototype=grand;function Father(){this.name="jjjj";}
var father=new Father();Son.prototype=father;
function Son(){}
var son=new Son();

son对象可以继承前面所有父类的属性方法

借用构造函数

不能继承借用构造函数的原型

​ 每次构造函数都要多走一个函数

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();

共有原型

Father.prototype.lastName="Deng";function Father(){}function Son(){}Son.prototype=Father.prototype;  //直接将父类的值,放到子类里var son=new Son();

缺点:不能随便改动自己的原型

​ 多个构造函数可以共用一个原型

Father.prototype.lastName="Deng";function Father(){}function Son(){}function inheri(Target,Origin){Target.prototype=Origin.prototype;  用一个方法实现共有原型}Son.prototype.sex="male";Inheril(Son,Father);var son=new Son();var father=new Father();  //发现从father对象也可以访问sex属性

圣杯模式

*最完美的继承模式*

​ 是基于共有原型来

function Father(){}function Son(){}function inherit(Target,Origin){function F(){};F.protetype=Origin.prototype;  //用F方法间接传原型,Target.prototype=new F();Target.prototype.constuctor=Target;  //表示出Son的父类指向Target.prototype.uber=Origin.prototype;  //用于存储Son的父类,}Inherit(Son,Father);var son=new Son();

该继承模式优势是可以单向创建属性互不影响

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MR0zhh4T-1593358772860)(D:\图库\作业\constucror.png)]

construtor为该方法属性,用于表示该方法的父类指向

另一种圣杯模式写法(继承模式)

function Father(){}function Son(){}var inherit=(function(){var F=function(){};return function(Target,Origin){F.prototype=Origin.prototype;Target.prototype=new F();Target.prototype.constuctor=Target;  //表示出Son的父类指向Target.prototype.uber=Origin.prototype; }
}());Inherit获得了return的方法Inherit(Son,Father);var son=new Son();

命名空间的优化管理

管理变量,防止污染全局,适用于模块化开发

var name="mmmm";  //外部就不会污染全局,两个name就不会互相污染var init=(function(){var name="kkkkk";function vurHast(){name="jojo";}function callName(){console.log(name);}						//写一个立即执行函数,可以将内部函数或变量返								回到一个全局变量里,这样名字和函数就不会污染								全局return function(){callName();vurHast();}}());

模块与模块之间变量名就不会相互污染,将功能放到闭包里面

var 	deng ={wife1 :{name :"xiaoliu"}wife2 :{name :"xiaofnag"}wife3 :{name :"xiaowang"}sayWife : function(num){return this['wife'+num];  //知识点在笔记对象上}}//调用时使用	  //字符串拼接deng(1);

利用字符串调用对象中的函数,

该知识点在,对象笔记上

对象方法字符串调用方法


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部