zoukankan      html  css  js  c++  java
  • 原型链-继承

    1、静态属性不能被继承

    2、每个对象都有一个原型属性__proto__,如果没有显示声明这个值默认为Object.prototype

    3、内部属性[Prototype] 通过 __proto__,静态属性 prototype

    4、IE浏览器不支持直接使用 __proto__

    5、代码中建议用Object.getPrototypeOf() 获取,不建议直接使用__proto__

    6、构造函数的实例对象都拥有指向其构造函数的Constructor属性

    var obj2 = {
        name: 'aaa'
    }
    console.log(obj2.__proto__ === Object.prototype) // true

    -----------------------------------------
    var base = {
        age: 13
    }
    var obj2 = {
        name: 'aaa',
        __proto__: base
    }
    console.log(obj2.age) // 13

    ----------------------------------------

      var base = {
        age:13
      }
     var obj2 = Object.create(base);
      console.log(g.age); //13

    function Person () {
        this.name = "xx";
    }
    function Son () {}
    Son.prototype = new Person();
    var s = new Son();
    console.log(s.name) // xx
    ------------------------------------
    function Person (name) {
        this.name = name
        this.show = function () {
            return 1;
        }
    }
    function Son (name) {
        Person.apply(this,arguments)
    }
    Son.prototype = new Person();
    var s = new Son("ada");
    console.log(s.name);
    console.log(s.show());

    5、原型链

    function People() {}
    function Person(){}
    var f = new Person();
    console.log(f.__proto__ === Person.prototype)
    console.log(Person.__proto__ === Function.__proto__)
    console.log(Function.prototype.__proto__ === Object.prototype);
    console.log(Object.prototype.__proto__  === null)

    console.log(f.constructor === Person)
    ------------------------------------------------ function People() {} function Person(){} Person.prototype = new People(); var f = new Person(); 当Person 继承 People : Person.__proto__ 指向发生改变 console.log(f.__proto__ === Person.prototype) console.log(Person.__proto__ === People.__proto__) console.log(People.__proto__ === Function.prototype) console.log(Function.prototype.__proto__ === Object.prototype); console.log(Object.prototype.__proto__ === null)

    console.log(f.constructor === People)
    function Mon() {
        this.name ="ada"
    }
    Mon.prototype.age = 34;
    Mon.prototype.show = function () { } ;
    console.log(b.constructor === Mon) //true
    ------------
    construct 不再指向构造函数
    Mon.prototype = {
        age: 34,
        show: function () { }
    }
    console.log(b.constructor === Mon) //false
    需要添加:
    Mon.prototype = {
        constructor: Mon,
        age: 34,
        show: function () { }
    }    

    6、判断对象是否在指定对象的原型链中:isPrototypeOf

    function People() {}
    function Person(){}
    Person.prototype = new People();
    var f = new Person();
    console.log(People.prototype.isPrototypeOf(f)) // true
  • 相关阅读:
    用C++实现从键盘输入两个数a和b,求两数中的最大值
    MongoDB学习笔记-1
    linux 配置ip地址
    linux 配置jdk 环境变量
    VMware Linux 共享文件夹 虚拟机无共享文件解决方法
    数据库动态参数
    js
    js分页
    mysql存储过程
    webconfig 中配置上传文件大小
  • 原文地址:https://www.cnblogs.com/yuyedaocao/p/11988916.html
Copyright © 2011-2022 走看看