Value lookup for JavaScript objects

Source: Internet
Author: User

Value lookup rules for JavaScript objects:

    • Lookup Order
    1. The object itself
    2. The prototype of the object constructor
    3. The prototype constructor of the object constructor is prototype in such a way that it is searched along the prototype chain until the prototype of object
    • Not found in the entire search chain. Returns undefine
    • Returns the found value once found in the lookup process
1Object.prototype.foo = ' Bar ';2 3 functionpeople () {}4People.prototype.kind = ' People ';5 6 functionChinese () {}7Chinese.prototype =Newpeople ();8Chinese.prototype.constructor =Chinese;9Chinese.prototype.country = ' China ';TenChinese.prototype.name = ' Chinese '; One  A varZhangsan =NewChinese (); -Zhangsan.name = ' Zhang San '; -  theConsole.log (Zhangsan.name);//Zhang San, from Zhangsan object,[)--the setting of name in chinese.prototype is not searched -Console.log (Zhangsan.country);//China , from Chinese ' s prototype -Console.log (Zhangsan.kind);//people, from person ' s prototype -Console.log (Zhangsan.foo);//bar, from Object ' s prototype +Console.log (Zhangsan.age);//undefine

Value lookup for JavaScript objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.