• JavaScript原型链


    JavaScript使用原型链来解析属性值。原型链描述了JavaScript引擎如何从对象查找到原型以及原型的原型,来定位对象的属性值。当请求对象的属性时,JavaScript引擎首先直接在该对象上查找。如果找不到该属性,则查找原型(保存在对象的_proto_属性中)查看原型是否包含了请求的属性。如果JavaScipt引擎在对象的原型上找不到该属性,它就查找原型的原型(原型只是一个对象它也有原型)。依次类推,当JavaScript到达通用的(generic)Object原型,原型链就结束了。如果JavaScript在原型链上的所有地方都找不到请求的属性,则返回undefined。由于JavaScript引擎会检查原型链,具体细节可能变得错综复杂,但是对于我们只需记得如果在对象上找不到属性,则检查它的原型。

    可以使用_proto_属性,手动的在原型链上“往上爬”。

    //浏览器兼容Object.create方法
    var objectCreat = function ( arg ) {
      if ( ! arg ) { return {}; }
      function obj() {};
      obj.prototype = arg;
      return new obj;
    };
    
    Object.create = Object.create || objectCreate;
    
    //1.定义原型的对象
    var proto = {
      sentence : 4,
      probation : 2
    };
    //2.定义对象的构造函数
    var Prisoner = function(name, id){
      this.name = name;
      this.id = id;
    };
    //3.将构造函数关联到原型
    Prisoner.prototype = proto;
    //4.实例化对象
    //使用 Object.create 的常见工厂模式是使用工厂函数来创建并返回最终的对象
    //所有的工厂函数我们以make<object_name>的形式进行命名
    var makePrisoner = function( name, id ) {
      var prisoner = Object.create( proto );
      prisoner.name = name;
      prisoner.id = id;
      return prisoner;
    };
    
    var firstPrisoner = makePrisoner( 'Joe', '12A' );
    //原型链请求:firstPrisoner对象上请求
    //如果请求firstPrisoner.name,
    //JavaScript会直接在对象上找到囚犯的名字并返回joe
    console.log( firstPrisoner );
    console.log( firstPrisoner.name );
    //原型链请求:firstPrisoner对象的firstPrisoner._proto_原型上请求
    //如果请求firstPrisoner.sentence,JavaScript在对象上找不到该属性,
    //但在原型上找到了它,返回值为4.
    console.log( firstPrisoner.sentence );
    //原型链请求:firstPrisoner对象的firstPrisoner._proto_原型的
    //firstPrisoner._proto_._proto_原型上请求
    //toString()在对象和他的原型上都没有,所以查找原型的原型,
    //正好是JavaScript的基础对象(base object 的方法)
    //得到的是字符串[object Object]
    console.log( firstPrisoner.toString() );
    //hopeless在对象上没有定义,在原型上没有定义,原型的原型上也没有定义,
    //因此它的值是undefined
    console.log( firstPrisoner.hopeless);
    var secondPrisoner = makePrisoner( 'Sam', '2BC' );
    

    在node下执行输出如下:

    { name: 'Joe', id: '12A' }
    Joe
    4
    [object Object]
    undefined
    
  • 相关阅读:
    这个星期一好像有点无聊
    SQL、LINQ、Lambda三种方式比较
    [C#基础知识]Class和Struct的区别
    C#判断大批量的路径是否存在时,如何缩短判断时间
    批处理 for参数之token详解
    批处理Delims的基本知识
    读取txt文本中每一条路径(一行)并判断,路径不存在的保存在另一txt文本中
    批处理获取一些系统参数
    Cannot read configuration file due to insufficient permissions
    my97datepicker日期选择插件的用法
  • 原文地址:https://www.cnblogs.com/nodejsxxh/p/4420214.html
Copyright © 2020-2023  润新知