JavaScript authoritative Guide Learning notes find an object on the third day

Source: Internet
Author: User
Tags hasownproperty

Real life of the real object did not find, in JavaScript left an object to another object, and I was dizzy to the unintelligible. Personnel are complex and people are difficult to understand. Although I am a liberal arts student, but also not good at Blarney. Or in the JS inside look for the object. So we start with the JS object today.

Yesterday, we talked about the data types in JS are divided into two kinds, primitive type, object type. The composition of the object type is made up of both the original type and the object type.

Let's take a look at some of the characteristics of an object today. Use object is nothing but increment (crate), delete (delete), change (set), check (query);

object is composed of key-value pairs, then the object's properties (key) have some related properties: Writable Arrtibute (writable), Enumerable Attribulte (enumerable), configurable attribute (configurable)

The object itself is also useful for three related object features prototype (the object's prototype), Class (object's Class), and (extensible) extended markup for the object.

The following focus on these attributes and characteristics of the JS object in-depth understanding:

There are three types of JS objects:

1, JS built-in objects,

2, the host object. such as the Window object.

3. Objects created by the user themselves.

There are two properties of JS:

1, is the current object's own properties. (Own property)

2, is an inherited property. (Inherited property)

First, create the object

There are three ways of creating objects:

1, by the direct amount of the object of the way to create.  For example var obj = {X:1}; The second day of the course has been explained, what is the direct amount, can go under reference.

2. Create with the New keyword. For example var obj = new Array ();

3. Create using Prototypes. Object.create ({x:1});

So what's different about these three kinds of objects, in fact, the difference is that they inherit different prototypes. An object prototype is inherited by using the direct amount of objects. An object created with the New keyword inherits an array object, and then the array object inherits the object. This is an embodiment of the JS prototype chain. Use the prototype chain to create an inherited prototype object;

Second, the prototype chain

Repeated in the book to mention this prototype chain, in fact, there is no complicated thing, is repeatedly stressed complex, to create a psychological complex feeling, can be used in simple words to describe clearly.

The prototype chain is like a gene. Grandpa passed the gene to his father, father passed the gene to his son, and his son passed the gene to his grandson. Fathers can produce their own genes, and sons can produce their own genes. Oh, that's roughly the case.

Third, property access

There are two ways to access an object. can be passed. the symbol accessor. can also be accessed by []. Use. Access, which triggers an error when the property is a reserved word or a keyword in the system. But [] You can use System keywords and reserved words. However, you should try to ensure that the object's properties do not apply to system reserved words.

Iv. Deleting attributes

var obj = {X:2,y:4};

Delete obj.x;

Deleting an attribute means that the current object can delete an object that it has created. However, you cannot delete a property through inheritance, that is, you cannot delete a parent property.

Delete keyword

Five, detection properties.

var key in obj

Obj.hasownproperty (' key '); Determines whether the property is an attribute of its own, that is, not an inherited property.

<script>        var obj = {x:2,y:3};         if (Obj.hasownproperty (' x ')) {            alert (' yes ');        }     </script>

Obj.propertyisenumerable (' x '); To determine if the property can be enumerated, the concept of the enumeration is said to be that it can be traversed by the for/for in loop.

var obj = {x:2,y:3};         if (Obj.propertyisenumerable (' x ')) {            alert (' yes ');        }

VI. Enumeration properties:

For with a in loop. is enumerated.

Seven: Set and get

Characteristics of attributes:

Value (value), writable (writable), enumerable (enumerable), configurable (configurable)

Object.getownpropertydescriptor (Obj,var) Gets the list.

<script>        var obj = {x:2,y:3};         var msg = object.getownpropertydescriptor (obj, ' x ');        Console.dir (msg);     </script>

Object.defineproperty (Obj,var,arr); Set a single property

<script>        var obj = {x:2,y:3};         var status  =  object.defineproperty (obj, ' x ', {configurable:false, Value:7});         var msg     =  object.getownpropertydescriptor (obj, ' x ');        Console.dir (msg);     </script>

Object.defineproperties (); Modify multiple properties at once

<script>        var obj = {x:2,y:3};         var status  =  object.defineproperties (obj,{            x:{value:100,writable:false, Enumerable:false, configurable:false},            y:{value:99,writable:false , Enumerable:false, configurable:false}        );         var msg  = Object.getownpropertydescriptor (obj, ' x ');        Console.dir (msg);     </script>

9. Three properties of an object:

The prototype property.

Viewing the prototype of an object can be viewed by object.getprototypeof (obj) to see which properties of the prototype have been inherited.

<script>        var obj = {x:2,y:3};  Creates an object from the object's        direct volume,var msg = object.getprototypeof (obj);        Console.dir (msg);     </script>

Because the object we just created is var obj = {X:2,y:3};

If we do not forget, the way we create objects is called, created using the direct amount of the object. You can refer to the first knowledge point of this article for three ways to create an object. The object is created by using the object's direct amount, which is called the object prototype, which is object.

A ispropertyof () can be used to set whether an object is a prototype of another object. The usage is p.isprototypeof (OBJ).

Class Properties:
Temporarily useless.

Scalability.

You can use Object.isextensible () to determine whether an object is extensible.

You can make the object non-extensible by Object.preventextensions ();

Once an object becomes non-extensible, it cannot be converted to extensible.

JavaScript authoritative Guide Learning notes find an object on the third day

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.