Chapter III Objects (JavaScript: The Essence of language)

Source: Internet
Author: User
Tags hasownproperty

An object is a container for properties, where each property has a name and value. 3.0. Overview: Object Literals | Search | Update | References | Prototypes | Reflection | Enumeration | Delete | Reduce global variable pollution by 3.1. Object literal an object literal is 0 or more "name/value" pairs enclosed in a pair of curly braces.
// 3.1 Code 1 var empty_object = {}; var stooge = {    "first-name": "Jerome",    "last-name": "Howard"};
objects can be nested:
// 3.1 Code 2 var filght = {    "oceanic",    815,    departure: {        "SYD",        "2004-09-22 14:55",        "Sydney"    },    arrival: {        "LAX",        "2004-09-23 10:42",        "Los Angeles"    }}
3.2. Retrieve the values contained in the retrieved object in two ways: [] and. 。
// 3.2 Code 3stooge["First-name"  / / "Jerome"//  "SYD"
Retrieves the value of a member property that does not exist, and returns undefined.
// 3.2 Code 4 // undefinedflight.status         //  undefinedstooge["first-name"]  //  undefined
by | | operator to set the default value:
// 3.2 Code 5 var middle = stooge["Middle-name"] | | "(none)"; var status = Flight.status | | "Unknown";
Avoid errors with the && operator:
// 3.2 Code 6flight.equipment                           //  undefinedflight.equipment.model                     // throw "TypeError" // undefined
3.3. The values in the Update object can be updated by an assignment statement.
stooge[' first-name ' = "Jerome"; stooge[' middle-name '] = "Lester"= {    "Boeing 777"  = "overdue";
If the property name exists, the property value is replaced, and if the property name does not exist, the property is extended to the object. 3.4. Reference objects are passed by reference.
// 3.4 Code 8 var x == "Curly"; stooge.nickname;             // "Curly" var // refer the diff Objecta = b = c = {};             // refer the same Object

3.5. Prototypes all objects created by literal literals are connected to object. Prototype We add a Create method to object and use this method to create a new object that uses the original object as its prototype.
// 3.5 Code 9 if (typeof Object.beget!== ' function ') {    function  (obj) {        var function  () {};         = obj;         return New Func ();    };    } var new_stooge = object.create (stooge);
It is not possible to update the property values of the prototype, only overwrite (the newly added attribute name is already in the prototype).
// 3.5 Code Tennew_stooge[' first-name '] = ' Harry '; new_stooge[' middle-name '] = ' Moses '= ' Moses ' ;    
The prototype object was changed, and it worked for New_stooge.
// 3.5 codeOne stooge.profession = ' actor '//  ' actor '
3.6. Reflection uses the typeof operator to determine the type.
// 3.6 Code typeof Flight.number   //  ' number 'typeof Flight.status   //  ' String 'typeof Flight.arrival  //  ' object 'typeof//  ' undefined '
Attribute types in the prototype:
// 3.6 Code typeof Flight.tostring    //  ' function 'typeof//  ' function '
If a property is a property unique to an object, it returns true with the hasOwnProperty method, and False if a property is a property on an object's prototype chain.
// 3.6 Codeflight.hasownproperty (' number ')      //  true//  False 
3.7. The enumeration for in statement iterates through all the property names in an object----include properties in functions and prototypes----Now you want to filter them, you can filter the properties in the prototype using the hasOwnProperty method, and use typeof to exclude the function.
// 3.7 Code var name;  for inch New_stooge) {    if (typeof New_stooge[name]!== ' function ' | |!  New_stooge.hasownproperty (name)) {        + ': ' + new_stooge[name]);}    }
But this traversal of the property name order is not deterministic. If you want to be sure, you need to use an array and a for statement.
// 3.7 Code var i; var properties = [    ' First-name ',    ' Middle-name ',    ' Last-name '  ,    ' profession '];  for (i = 0; i < properties.length; i + = 1)    {+ ': ' + new_stooge[properties[i]]);}

3.8. Remove the DELETE statement to delete the object's properties.
// 3.8 Codenew_stooge.nickname         //  ' Moe ' object ' s valuedelete  new_ Stooge.nickname;new_stooge.nickname         //  ' Curly ' exposed prototype ' s value
3.9. Reduce global variable pollution create a unique global variable.
// 3.9  Code-var MYAPP = {};
This variable becomes the container for your application:
// 3.9 Code Myapp.stooge = {    "first-name": "Jerome",    "last-name": "Howard"= {    " Oceanic ",    815,    departure: {        " SYD ",        " 2004-09-22 14:55 ",         "Sydney"    },    arrival: {        "LAX",        "2004-09-23 10:42",         "Los Angeles"    }}
Such a global resource is included under a namespace. The way to use closures for information hiding is also an effective way to reduce global variables.  Here's what it says. Finish

Chapter III Object (JavaScript: Language Essence)

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.