Javascript-object Basic Knowledge

Source: Internet
Author: User

1. Definition: Object is the core concept of JS and the most important data type. All data of JS can be considered as objects. An object is an unordered collection of data, consisting of several key-value pairs (key:value), wrapped by {}; 2. Notation: var obj={Property Name: property value,Property Name: property value,Property Name: Property value The last key value is not recommended to add a comma (IE8 incompatible);}; A, all the key names of an object are strings, so you can write without quotation marks. B, the key name is also called "property", its key value can be any data type. C, if the value of an attribute is a function, it is usually called a "method", which can be called like a function.  Note: (1) key names to conform to the identifier naming specification;(2) If it does not conform to the "";(3) The key name can be a numeric value, it will automatically be converted to a string;(4) Key name can be JS keyword (reserved word), but not recommended; 3. How to create an object:(1) Literal method definition: var obj={};(2) constructor creation: Var obj=new Object ();(3) based on the prototype (inheritance mode): Var obj=object.creat (null); (not commonly used) 4. Read and Write Properties (1) Read attribute: A, dot operator------object name. property name;B, bracket output-------Object name [' property name '];Note: (1) The numeric key name cannot be read with the dot operator (because it is treated as a decimal point),(2) numeric keys can only be read using the square bracket operator, which is not quoted (because it is automatically converted to string processing). (3) The property name in square brackets must be quoted (number key is arbitrary); (2) write attribute (Assignment): A, the point attribute is written to obj. property name = attribute value;(unordered) B, [] form write obj[' property name ']= attribute value;JS allows properties of the ' post-bind ' property, which can be created dynamically; 5. View All properties: the object. Keys (obj) returns a value of all the property names in the object, and the result is a collection of array of property names; 6. Delete the attribute: Delete obj. property name deletion succeeds return value true, otherwise false;Delete and then go to view the property, the return value is undefined;Delete a non-existent property, no error, return value is true;the Delete command can only delete properties of the object itself. You can use hasOwnProperty to determine whether the property of the object itself.  7. Object reference: Different variable names point to the same object (referencing the same object), modify one of the variables, will affect all other variables;
var  o1 = {},
O2 = {name:123}
O1 = O2;
O1.name; 123
O2.name = 456;
O1.name; 456
8.in operator: Used to detect whether an object contains a property (key name);
             The property name  in obj (' name ' in P) contains a return value of true, otherwise returns false;
9.for...in (unordered): Iterates through all the properties of an object, returning----property values;
for (var i in P) {
    Console.log (P[i]);
};
10.with: Manipulate multiple properties of the same object, reduce the amount of code, avoid repeating writing (similar to while);
   With (object) {
      Property name = attribute value;    with statement block, all assigned statements are appended with semicolons;
     name = 1000;
Age = 3;
    }
     Note: (1) View object.name, return value is undefined;
           (2) With block on name, equals create a global variable name, which is not part of the object
You can define the property name of the object first, and then manipulate it within the With block;
           (3) The property of the object can be re-assigned to override the original property values;
<-----------Study notes, thank you for correcting me! ----------->

Javascript-object Basic Knowledge

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.