Introduction to JavaScript Creation object and object prototype chain properties

Source: Internet
Author: User
Tags object object hasownproperty

We know a way to define a common object in JavaScript, such as:

 Let obj == 1= ' string 'function() {alert (' func '= [' x ', ' Y ']; Console.log (obj.num);     // 1console.log (obj.string);    // "string"Console.log (obj.func);    // function () {alert (' func ')}Console.log (Obj.arr);    // ["X", "Y"]

Or:

 Let obj = {      1,      ' string ',      function() {alert (' func ')},      arr: [' x ', ' Y ']}

Constructor method:

function animal () {    this. Name = ' Animal'new  animal (); Console.log ( Obj.name);     // "Animal"

Wait a minute.

However, each object has its own prototype attribute, for example, we add a prototype attribute to an object:

function animal () {    = ' animal ';     = 1new= ten; Console.log (obj.name);    // ' Animal 'console.log (obj.x);   // Ten // 1

Above we have added the name and X property to the Animal object prototype, and in the following new constructor, obj, it inherits the animal object itself, so we can find the Name property value in obj, but once obj gives animal the attribute ' x ' When the value of the value is re-assigned to 10, it is changed, but the ' X ' value on the animal prototype is unchanged, because the property in obj is the first to find its own defined property, if not found, will go to its prototype chain to find, that is, animal.

Let's take another look at the case:

Console.log (typeof//  "function" in obj)    //true    ' in ' Keyword will also find its prototype chain up, so name is present Console.log (Obj.hasownproperty (' x '))   //  true Console.log (Obj.hasownproperty (' name '))    //false

Why does the ToString method come from the Obj object and the animal object we just didn't define the ToString method? This is also a priority issue, and it will always look up from obj until this property is found, and if no will return undefined, each Object object prototype Ririe has the ToString method by default. The hasOwnProperty () method is used to determine whether an object has a property or object that you give a name to, it uses it to judge the current object, and cannot determine whether the property exists on the prototype chain of the current object, because Obj does not define the Name property, and all returns FALSE.

Another method for inheriting the prototype chain of an object is introduced:

New Object ({    ' cat ',    y:+ == 1; Console.log (Obj.hasownproperty (' X '))  //trueconsole.log (obj.hasownproperty (' y '))  //false Console.log (obj.name)    //catconsole.log (OBJ.Y)/    /

Introduction to JavaScript Creation object and object prototype chain properties

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.