zoukankan      html  css  js  c++  java
  • 你不知道的JS系列上( 47 ) - [[Prototype]]

    JS中的对象有一个特殊的 [[Prototype]] 内置属性,其实就是对于其他对象的引用。几乎所有的对象在创建时,[[Prototype]] 属性都会被赋予一个非空的值。
    var myObject = {
      a:2
    }
    myObject.a;
    [[Prototype]] 引用有什么用呢?比如当你试图引用对象的属性时会触发 [[Get]] 操作,比如 myObject.a,会触发 [[Get]] 操作,第一步是检查对象本身是否有这个属性,如果有的话就使用它。如果没有,就使用对象的 [[Prototype]] 链了。如果还么有,返回值是 undefined
    var anotherObject = {
    a: 2
    }
    var myObject = Object.create(anotherObject);
    myObject.a; // 2

    Object.create(...) 会创建一个对象并把这个对象的 [[Prototype]] 关联到指定的对象。现在 myObject 对象的 [[Prototype]] 关联到了 anotherObject。myObject.a 并不存在,但是还是找到了 2。

    var anotherObject = {
      a: 2
    }
    var myObject = Object.create(anotherObject);
    for(var k in myObject){
      console.log('found: ' + k)
    } // found: a
    ('a' in myObject); // true

    for...in 遍历对象原理和查找 [[Prototype]] 链类似,任何可以通过原型链访问到的属性都会被枚举。因此,通过各种语法进行属性查找时都会查找 [[Prototype]] 链,直到找到属性或者查找完整条原型链

  • 相关阅读:
    get pc name in LAN
    study spring
    android install
    【转】Java:Session详解
    classic problem: producer and consumer
    tomcat install
    验证Monitor.PulseAll功效
    (转)Windows7的图形架构与DX的那点事
    Cannot open your default email folders. The attempt to log on to Microsoft Exchange has failed.
    Monitor.Wait初探(8)
  • 原文地址:https://www.cnblogs.com/wzndkj/p/12664312.html
Copyright © 2011-2022 走看看