JavaScript objects (object)

Source: Internet
Author: User
Tags hasownproperty

JavaScript's simple data types include numbers, strings, Boolean values, null values, and undefined values, all of which are objects.  Numbers, strings, and Boolean "seemingly" objects because they have methods, but they are immutable. The object in JavaScript is a mutable keyed collection.     In JavaScript, an array is an object, a function is an object, a regular expression is an object, and of course an object is naturally an object. Objects are containers for properties, where each property has a name and value. The name of the property can be any string, including an empty string. The property value can be any value other than the undefined value. JavaScript objects are untyped (Class-free). It has no restrictions on the value of the new property's name and property. objects are suitable for pooling and managing data.  Objects can contain other objects, so they can be easily identified as either a tree or a graphical structure. JavaScript contains the characteristics of a prototype chain that allows an object to inherit the properties of another object.  Using it correctly can reduce the time and memory consumed by object initialization. JavaScript objects are represented using a pair of 0 or more property name/property value pairs in curly braces. When the property name is legal, it does not enforce that the value of the quotation mark property can be obtained from any expression that includes another object literal. objects can be nested. Instance
var stoge = {     "first-name": "Jerome"}var flight = {     "oceanic" , number     :815,     departure:{         IATA:"SYD", Time          :"2016-01-01 14:55" , City          :"Sydney"      },     arrival:{         IATA:"LAX", Time          :" 2016-01-02  10:42 ", City          :" Los Angeles "      }}

Retrieving RetrievalTo retrieve the values contained within an object, there are two ways of [] and. 。 [] is the way in which a string expression is enclosed in an object suffix. If the string expression is a string literal, and it is a valid JavaScript identifier and is not a reserved word, it can be used. notation instead. Priority is given to use. notation, because it is more compact and more readable. Instance
stoge["First-name"]     // return to "Jerome"Flight.departure.IATA  // return "SYD"
If you retrieve a value for a member property that does not exist, return undefined
stoge["Middle-name"]  // return to undefined
|| You can populate the default values
var // return "{none}"
&& can avoid errors (taking values from undefined attribute members will result in TypeError exceptions)
Flight.equipment               //undefinedflight.equipment.model     //throw "TypeError"  //undefined
Updating updateThe values in the object can be updated by the copy statement. If the property name already exists in the object, then the value of the property is replaced, and if the object does not have that attribute, ING, then the attribute is extended to the object.
stoge["first-name"] = "LIUSXG";
Reference ReferenceObjects are passed by reference, they are never copied
var x == ' Curly '; var nick = stoge.nickname;      // because X and Stoge are references to the same object, Nick is ' Curly ' var a = {}, b = {}, c = {};      // because A, B, and C each reference a different empty object A = b = c = {} '     //a, B, and C all reference the same empty object
prototype prototypeEach object is connected to a single prototype, and it can inherit properties from it.  All objects created by object literals are connected to Object.prototype, which is the standard object in JavaScript. When you create a new object, you can select an object as its prototype. JavaScript provides an implementation mechanism that is messy and complex, but can be significantly simplified. We have added a create method to Brother Jiang object. This method creates a new object that uses a meta-object as its prototype.
var another_stoge = object.create (Stoge);
The prototype connection is not functional at the time of the update. When we make that table on an object, we don't touch the prototype of the object.
another_stoge["first-name"] = ' Harry ';
The prototype connection is only used when retrieving values. If we try to get a property value for an object, but the object does not have this property value, then look for it from its prototype, and so on, knowing that the process finally reaches the end of Object.prototype. If the desired attribute does not exist in the prototype chain at all, the result is the undefined value.  This process is called a delegate. The prototype relationship is a dynamic relationship. If we add a new attribute to the prototype, the property is immediately visible to all objects based on that prototype.
stoge.profession = ' action '; another_stoge.profession  //' action '
Reflective ReflectionIt is easy to examine an object and determine what properties it has, just try to retrieve the property and verify the obtained value.
typeof Fight.number  //' number 'typeof//' string '  typeof//' undefined '
Note that any attribute in the prototype chain will be generated:
// ' function ' // ' function '
There are two ways to get rid of these unwanted properties.  The first is to have your program check and discard the properties of the function, in general, when you want the object to dynamically get its own information at run time, you focus more on the data, and you should be aware that some values may be functions. Another method is to use the hasOwnProperty method, which returns true if the object has a unique property. The hasOwnProperty method does not check the prototype chain.
Flight.hasownproperty (' number ')     //trueflight.hasownproperty (' construction ')     //  True

Enumeration EnumerationThe for in statement can be used to traverse all property names in an object. The enumeration process will list all of the properties-including the ones in the function and the prototypes you might not care about-all of which are necessary to filter out the values you don't want. The most commonly used filters are the hasOwnProperty method, and the use of typeof to exclude function instances
var name;  for inch Another_stoge) {     if(typeof Another_stoge[name]!== ' function ') {         + ': ' +  Another_stoge[name]);}      }
The order in which property names appear is indeterminate, so be prepared for any order that might occur. If you want to make sure that properties appear in a particular order, the best way is to create an array placement property name instead of using the for in statement, and to get the desired property value instance with a For loop
var i; var properties = [     ' First-name ',     ' Middle-name ',     ' profession '] ;  for (i = 0; i < properties.length; i++) {     + ': ' +                      another_stoge[properties[i]]);}
This gives you a direct access to the properties we need without worrying about the possibility of discovering the properties in the prototype chain. Remove DeleteThe delete operator can be used to delete an object property. If the object contains this property, the property is removed.  It does not touch any object in the prototype chain. Deleting an object's properties may cause the properties in the prototype chain to come out,
Another_stoge.nicknam     //' Moe '// Delete Another_stoge's nickname property, Thus exposing the nickname properties of the prototype.  Delete  another_stoge.nickname;another_stoge.nickname     //  ' Curly '
reduce global variables pollution abatement GlobalJavaScript can easily define the global to accommodate all the resources of your app.  But global variables weaken the flexibility of the program. One way to minimize the use of global variables is to create only one unique global variable for your app
var MYAPP = {};
This variable is now a container for your application
Myapp.stoge = {     "first-name": "Joe",     "last-name": "Howard"= {     " Oceanic ", number     :815,     departure:{         IATA:" SYD ", Time          :" 2016-01-01 14:55 ", City          :" Sydney "      },     arrival:{         IATA:" LAX ", time          : "2016-01-02  10:42", City          :"Los Angeles"      };
As long as the global resources are included in a namespace, there is a significant reduction in the likelihood that your program will conflict with other applications, components, or library classes. Closures can also be used to reduce global pollution.

JavaScript objects (object)

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.