zoukankan      html  css  js  c++  java
  • JavaScript 继承

    JavaScript实现继承的几种方式

    原型链最基本的一种继承--过多继承了没用的属性

     1 Grand.prototype.lastName = "Deng";
     2         function Grand() {
     3 
     4         }
     5         var grand = new Grand();
     6 
     7         Father.prototype = grand;
     8         function Father() {
     9             this.name = "123";
    10             this.fortune={
    11                 a:123
    12             }
    13         }
    14         var father = new Father();
    15 
    16         Son.prototype = father;
    17         function Son() {
    18             this.hobbit="smoke";
    19 
    20         }
    21         var son = new Son();
    View Code

    借用构造函数实现继承  -- 每次构造函数都要多走一个函数 无法继承借用构造函数的原型

      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("a",12,1,1);
    View Code

    共享原型---不能随便改动自己的原型

    Father.prototype.lastName="Deng"
            function Father(){
    
            }
    
            function Son(){
    
            }
            未封装
            Son.prototype=Father.prototype;
            var son = new Son();
            var father = new Father();
    View Code

    我们对共享原型模式加以封装改造

    function Father() {
    
            }
    
            function Son() {
    
            }
            //target 继承 origin
            function inherit(Target, Origin) {
                Target.prototype = Origin.prototype
            }
            //必须先继承后用
            inherit(Son, Father);
            var son = new Son();
    View Code

    圣杯模式(较好的继承实现,一般采用此方式)

    // function inherit(Target, Origin) {
            //     function F() {};
            //     F.prototype = Origin.prototype;
            //     Target.prototype = new F();
            //     Target.prototype.constructor = Target;
            //     Target.prototype.uber = Origin.prototype;
            // }
            //比较专业的写法
            var inherit = (function () {
                var F = function () {};
                return function (Target, Origin) {
                    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();
    View Code
  • 相关阅读:
    C语言 atoi
    C语言 strtok
    C语言 strstr
    Python从菜鸟到高手(6):获取用户输入、函数与注释
    Python从菜鸟到高手(5):数字
    Python从菜鸟到高手(3):声明变量
    Python从菜鸟到高手(2):清空Python控制台
    Python从菜鸟到高手(1):初识Python
    《Python从菜鸟到高手》已经出版,开始连载了,购买送视频课程
    Python从菜鸟到高手(1):数字的奥秘
  • 原文地址:https://www.cnblogs.com/FashionDoo/p/10602771.html
Copyright © 2011-2022 走看看