精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Javascript Object Hierarchy
userphoto

2014.04.24

關注
download full size image (for printing)


Also see: another great explanation of JS prototypes
(external link)


Interesting Points

  1. All instances inherit from the prototype object of thefunction that created them.
  2. Mozilla/Konqueror have an implementation-specific __proto__ property that points to the prototypeobject of the creator function (the function used to create anyinstance of that type).
  3. Regardless of the presence/absence of a __proto__property, the general idea is that all objects use theprototype object pointed to by that objects creatorfunction. This property is part of the Javascript standard and iscalled prototype. The prototypeobject, by default, has a constructor propertypointing back to the function that it's the prototype for.
  4. The prototype is only used for properties inherited byobjects/instances created by that function. The function itself doesnot use the associated prototype (but sincethe function itself is an object, it inherits from theprototype of it's creator function, typically the javascriptsystem "Function" object).
    function Foo() { } ; var f1 = new Foo();Foo.prototype.x = "hello";f1.x   //=> helloFoo.x //=> undefined
    Note, we use the Foo.prototype to set propertiesfor all objects created by function Foo. We don't sayf1.prototype to set properties for f1.This is a very important point to remember.
  5. Default prototype objects can be replaced with another usercreated object. While doing so, the constructor property must beset manually to replicate what the javascript runtime does behindthe scence with the default prototype object.
    function foo() { } ; var f1 = new foo();f1.constructor === foo.prototype.constructor === foo  //replace the default prototype objectfoo.prototype = new Object();//now we have:f1.constructor === foo.prototype.constructor === Object//so now we say:foo.prototype.constructor == foo//all is well againf1.constructor === foo.prototype.constructor === foo
  6. Each prototype object itself is created (by default) with theObject() constructor, hence the prototype has as it's prototypeObject.prototype. Therefore all instances regardless of the typeultimately inherit properties from Object.prototype.
  7. All objects automatically read properties in the prototypechain as-if those properties where defined in the object itself.

    Setting the same property via theobject shadows/hides the same property in theprototype for that instance.

    function foo() { } f1 = new foo();f2 = new foo();foo.prototype.x = "hello";f1.x  => "hello"f2.x  => "hello";f1.x = "goodbye";   //setting f1.x hides foo.prototype.xf1.x  => "goodbye"  //hides "hello" for f1 onlyf2.x  => "hello"  delete f1.xf1.x  => "hello";   //foo.prototype.x is visible again to f1.
    Setting the property directly in theprototype changes it for all instances.
    foo.prototype.x = "goodbye";//nowf1.x  => "goodbye"f2.x  => "goodbye";

More Interesting points

Viewing/following various arrows in the diagram above, you'll see some interesting relationships in the core javascript language. (Type these code examples in a javascript console if you want to play along).
  1. Function.__proto__ points to Function.prototype. This results in:
    Function.constructor === Function
    That is to say: Function is it's own constructor !
  2. Object instanceof Object == true.

    This is because:
    Object.__proto__.__proto__.constructor == Object

    Note also that unlike Object instanceof Object, Foo instanceof Foo == false.

    This is because: Foo does not exist as a constructor for it's own prototype chain.

  3. Function.prototype.toString is a built-in method and isdistinct from another built-in method: Object.prototype.toString
    f1.toString() finds:Object.prototype.toStringWe get something like: [object ...]
    Whereas:
    Foo.toString() first finds & uses:Function.prototype.toString()  We get something like: [Function foo...]
    If we say:
    delete Function.prototype.toStringFoo.toString()  We get something like: [object ...]
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JavaScript是如何面向對象的
js 對象布局
JavaScript: 使用面向對象的技術創建高級 Web 應用程序
構造函數與 new 命令
JavaScript 原型中的哲學思想
JavaScript 從類繼承
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 若羌县| 大余县| 修文县| 阿巴嘎旗| 台湾省| 洛阳市| 寿宁县| 宿州市| 沽源县| 苏尼特左旗| 谷城县| 瑞金市| 浮山县| 雅江县| 比如县| 广东省| 略阳县| 驻马店市| 星子县| 宝应县| 若羌县| 岳阳市| 建阳市| 陆川县| 包头市| 澄迈县| 黄陵县| 潍坊市| 张家界市| 青海省| 宝兴县| 安西县| 中牟县| 长武县| 兴宁市| 北海市| 壤塘县| 荣昌县| 恭城| 牡丹江市| 葫芦岛市|