• JavaScript继承


    1、传统继承形式 ——>原型链继承

      缺陷:过多的继承了没用的属性

    function SuperType() {
         this.property = true;
    }
    SuperType.prototype.getSuperValue
    = function() { return this.property; } function SubType () { this.property = false; ) //继承superType SubType.prototype = new SuperType(); //添加新的方法 SubType.prototype.getSubValue = function() { return this.property; } //重写超类中的方法
    SubType.prototype.getSuperValue = function() {
      return false;
    }
    var instance = new SubType(); console.log(instance.getSuperValue) //false

    2、借用构造函数

      不足:不能继承借用构造函数的原型;每次构造函数都多走一个函数

    function SuperType(name, age) {
      this.name = name;
      this.age = age;
    }
    function SubType(name, age, gender){
      SuperType.call(this, name, age);
      //SuperType.apply(this, [name, age]);
      this.gender = gender;
    }
    var instance = new SubType("huang", 18, "male");
    
    console.log(instance.name)  //"huang"
    console.log(instance.gender)  //"male"

    3、圣杯模式

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

    ***方法

     1、obj.hasOwnProperty(property)   判断对象是否有某个属性

     2、obj instanceof super  判断obj的原型链上是否存在super

     3、Object.create(原型)

        var obj = Object.create(null)  //这样构造的对象原型链为空

  • 相关阅读:
    动软代码生成器 修改配置
    显示转换explicit和隐式转换implicit
    Memcache学习整理
    SQL2008-分页显示3种方法
    SQL2008-表对表直接复制数据
    SQL2008-删除时间字段重复的方法
    SQL2008-中不想插入从复记录
    SQL2008-c:PROGRA~1COMMON~1SystemOLEDB~1oledb32.dll出错找不到指定的模块
    ACCESS-如何多数据库查询(跨库查询)
    ACCESS-字符函数
  • 原文地址:https://www.cnblogs.com/lianchenxi/p/9736558.html
Copyright © 2020-2023  润新知