zoukankan      html  css  js  c++  java
  • js继承模式

     1、传统形式 ----> 原型链
    过多的继承了没用的属性
    Grand.prototype.lastName = "1";
    function Grand(){
    }
    var grand = new Grand();
    Father.prototype = grand;
    function Father() {
    }
    var father = new Father();
    Son.prototype = father;
    function Son() {
    }
    var son = new Son();
     2、借用构造函数
    不能继承借用构造函数的原型
    每次构造函数都要夺多走一个函数
    function Person(name, sex, age) {
         this.name = name;
         this.sex = sex;
         this.age = age;
     }
    
     function Student(name, sex, age, grand) {
         Person.call(this, name, sex, age);
         this.grand = grand;
     }
    
     var student = new Student();
     3、共享原型     //缺点 :一个值改另一个也跟这改
    不能随便改动自己的原型
    Father.prototype.lastName = 'Deng';
    
     function Father() {
    
     }
    
     function Son() {
    
     }
     function inherit(Target , Origin){
         Target.prototype = Origin.prototype
     }
     inherit(Son , Father);
    var son = new Son();
    var father = new Father();
     4、圣杯模式
     function inherit(Target , Origin){
         function F(){}
         F.prototype = Origin.prototype;
         Target.prototype = new F();
         Target.prototype.constructor = Target;
         Target.prototype.uber = Origin.prototype;
     }
     Father.prototype.lastName = 'Deng';
     function Father() {
     }
    
     function Son() {
     }
    
     inherit(Son , Father);
     var son = new Son();
     var father = new Father();
    私有化变量的应用
     function Li(name , wife){
         var preareWife = "xiaozhang";
         this.name = name;
         this.wife = wife;
         this.divorce = function () {
             this.wife = preareWife;
         };
         this.changPrepareWife = function (target) {
             preareWife = target;
         };
         this.sayPrapreWife = function () {
             console.log(preareWife);
         }
     }
     var li = new Li("li" , "xiaoLi")
  • 相关阅读:
    office(Word、Excel、PPT等图标异常和桌面无新建解决方案)
    CentOS7安装搭建.Net Core 2.0环境-详细步骤
    Win7系统system进程句柄数一直增加解决方案
    安装.Net Framework 4.6.2时出现“无法建立到信任根颁发机构的证书链”解决方法
    css关于定位那些事情
    1份2015计划书
    js关于弹也遮罩层
    每日一句(2014-10-23)
    每日一句(2014-10-17)
    每日一句(2014-10-16)
  • 原文地址:https://www.cnblogs.com/punisher999/p/12219194.html
Copyright © 2011-2022 走看看