继承模式,对象空间,对象枚举

继承模式(圣杯模式)

继承的发展史

  1. 传统形式–> 原型链
    过多的继承了没用的属性
  2. 借用构造函数
    不能继承借用构造函数的原型
    每次构造函数都要多走一一个函数(通过call来改变this的指向)
        function Person(age,name,sex){
            this.age = age;
            this.name = name;
            this.sex = sex;
        }
        function Student(age,name,sex,grade){
            Person.call(this,age,name,sex)
            this.grade = grade;
        }
        var student = new Student(20,'xiaozhao','male',18);
        console.log(student);
  1. 共享原型
    不能随便改动自己的原型(son的protptype和father的prototype是一样的原型)
 Father.prototype.lastName = 'deng';
        function Father(){

        }
        var father = new Father();

        function Son(){

        }
        function inherit(Target,Origin){
            Target.prototype = Origin.prototype;
        }
        inherit(Son,Father);
        var son = new Son();
        console.log(son.lastName);

注意事项:继承必须在new之前,否则的话木已成舟。
4. 圣杯模式

Father.prototype.lastName = 'deng';
        function Father(){

        }
        var father = new Father();

        function Son(){

        }
        function inherit(Target,Origin){
            function F(){}
            F.prototype = Origin.prototype;
            Target.prototype = new F();
        }
        inherit(Son,Father);
        var son = new Son();
        console.log(son.lastName);

标准的圣杯模式

//圣杯模式
	function inherit(Target, Origin) {
	function F() {};
	F . prototype = 0rigin. prototype;
	Target. prototype = new F();
	Target. prototype. constuctor = Target;
	Target. prototype. uber = 0rigin. prototype ;
}

命名空间

功能:管理变量,防治污染全局,实现模块化开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值