JavaScript authoritative Guide Read Note 3

Source: Internet
Author: User
Tags hasownproperty

Sixth Chapter Object

1. The first is to introduce the direct amount of the object format: the direct amount of the object is 1. A mapping table consisting of several name/value pairs, 2/value pairs separated by a colon, 3 value pairs separated by commas, and 4 the entire mapping table is enclosed in curly braces. This makes up a direct amount of the object. The property name is an identifier or a string literal, and the value of the property can be any form of the JS expression, and the value of the expression is the value of the property. Is the form of objects that are normally touched, such as:

var a= {a:111,b:222}

The authors also cite an example of knowledge that requires special attention:

 

var b = {      "main title": "AAA",     // attribute has a space in its name, so it must be represented by a string       "sub-title": " BBB ",   // attribute name has hyphens, must be represented by string       " for ":" CCC ",//" for "is the system reserved word, so must be quoted        author:{       firstname:"Dava",       LastName:"AAA"    }}    

2. When building a new object, you can

var a = new Object ();  var B = new Array (); var c = new Date ();

where object (), Array (), and so on are constructors

3. You can also create a new object by using the Object.creat () method, for example:

var d = object.creat (e)//e is a prototype of D.

The concept of the object's prototype is somewhat similar to the concept of the parent class in Java, which can be seen in the following blog post:

Prototype objects and prototype classes:

Http://www.cnblogs.com/xqhppt/archive/2012/02/01/2334355.html

When you create an object D with E as a prototype, D{c: "CCC"} inherits the properties of E, for example E is {a: "AAA", B: "BBB"}:d. C is called "own property" of D, and D has both D.A and D.B properties, But these two properties will be shown as inherited from the prototype properties, but they are the attributes of the D, when you use D.A and d.b, JS will be found in the attributes of D, when JS in D did not find A and B properties, JS will be in its prototype E, after finding A and B, JS creates two d.a and D.B properties for Object D and operates on these values, but does not have any effect on the prototype object E.

4. There are two ways to access an object's properties, one that is passed as Java. To access, the right side of the point must be a simple identifier named after a property name, such as Book.author,book.name, and the other method is accessed by square brackets. The square brackets must be an expression that evaluates to a string or the result can be converted to a string, which is the name of the property, such as book["main title"]

The meaning of Object.property and object["property" is the same.

Use the. method to manipulate the properties of an object, and when we do not know the name of the object property, we manipulate the property by square brackets or get the name of the property, for example:

  

 for inch portfolio) {       var shares =Protfolio[stock];    }

This way we can use square brackets to facilitate the value of all the properties in an object, even if we don't know the name of the property.

5. When accessing an object, it is not always successful to return a value or set a property value, for example: When you access an object's nonexistent property, you get a undifined return value, and when you access an object that does not exist, JS will give you an error. The author gives us a way to avoid errors on both ends that cause the program to error:

  

Suppose you don't know if there is a book and book.title, but we want to get the length of Book.title:1. var leng= undifinedif(book) {    if(book.title) {        Leng= book.title.length;}    } 2var leng = book&&book.title&&book.title.length

Property assignment to null and undifined, also packet type error,

There are other cases where it is not possible to assign a normal value:

A. If the P property in O is read-only, you cannot assign a value to P

B. If the P attribute in O is inherited, but is also read-only, it cannot be overwritten

C. If there is no attribute P in O, there is no setter inherited from P, and p is not scalable, you cannot add a new attribute p.

6. You can delete a property of an object by deleting it, for example: Delete book.title,delete book["main title"], but delete delete property and the host object's contact (the author gives an example here to explain this sentence: A={p : {x:1}}), B=a.p,delete A.P. This is the value of b.x is still 1, that is to say delete only coral A and attribute P link, and did not imagine B and P contact)

Delete can only delete the free properties of an object and cannot manipulate properties that inherit from the prototype. If you delete a property of the prototype, it will also affect every object that inherits from the prototype.

Delete cannot delete an object that has the Flase property of the configurable properties.

In non-strict mode, you can omit references to global objects when you delete the configurable properties of a global object, such as:

This.x=1;

Deleete x;

However, in strict mode, you must add a reference to the global object to remove the configurable properties of the global object

This.x =1

Delete this.x;

A 7.JS object can be seen as a collection of attributes. When we need to detect whether an object has a property, we can use the In,hasownproperty () and propertyIsEnumerable () methods.

In determining whether an object exists in this property, such as:

var a ={x:1}

"X" in A; True

hasOwnProperty () Determines whether the property is an object's own property, and the property returned by the integration returns false

such as: O.hasownproperty ("x")//true o.hasownproperty ("toString")//false

8. The attribute is enumerable, and when the enumerable of the property is true, it can be facilitated by the For/in loop to this property, if the enumerable is flase, how can not, such as:

var o={x:1,y:2,z:3};o.propertisenumable (// error, non-enumerable for in  o) {    Console.log (//}

ECMAScript 5 provides two functions that can obtain the name of an enumeration property:

1:object.key (); Returns an array of the names of the enumerable properties of an object

2:object.getownproperty (): He returns an array of names that have all their own properties, including enumerable and non-enumerable.

9.JS provides a special memory property, which differs from "Data attribute", "Data attribute" is simply a value, "Memory Attribute" is composed of one or two methods, two methods are getter and setter, for example:

varp ={x:1, y:2, get r{returnMATH.SQRT ( This. x* This. x+ This. y* This. y);}, set R (newvalue) {varOldValue = Math.sqrt ( This. x* This. x+ This. y* This. Y)varRatio = Newvalue/oldvalue This. x*=ratio This. y*=ratio}}

When we call the P.R property

Console.log (P.R)// Call the Get R method p.r=5// Call the Set R method

10.

Attributes have four attributes: for data properties, values, writable rows, enumerable, and configurable. For "Memory Properties", read (get), write (set), enumerable, configurable, respectively.

JavaScript authoritative Guide Read Note 3

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.