JavaScript authoritative design--javascript object (Brief study note seven)

Source: Internet
Author: User

1.with StatementsSyntax:
Width (object) {    statement}
The WITH statement can be used to extend the scope chain temporarily . The scope chain can be sequentially retrieved by the list of objects through which you can perform variable name resolution. With adds an object to the head of the scope chain, then executes the internal statement, and finally restores the scope chain back to its original state. however: The WITH statement should be avoided as much as possible. statements with the with code are difficult to optimize and run more slowly. when objects are nested and multi-layered, they are usually optimized with a with. such as:
Document.forms[0].address.value//-with (document.forms[0]) {     // direct access to table cell element    name.value= "";    ....}
you do not have to add the document.forms[0] prefix to each attribute. This object is temporarily mounted on the scope chain . It can also be written like this:
var f=document.forms[0];f.name.value= "";

It is important to note that the scope chain is used only when the identifier is found and is not used when creating a new variable. such as:
 with (0) {    x=1;} // ->x=1
  2. Create an Objectobjects can be created by the object's direct volume, keyword, new, and object.create () functions method One: Object direct Volume Creation objectsuch as:
var emp={    x:0,    y:0}
The direct amount of an object is an expression that creates and initializes a new object for each operation of the expression.   mode Two: New Create objectNew is followed by a function call, where the function is called a constructor, and the constructor friendship initializes a newly created object. such as:
var o=New Object ();    // Create an empty object, like Var o={}
  Method Three: Object.create () function to create an objectThe prototype of the object created by the new Date () is Date.prototypeDate.prototype attributes are inherited from Object.prototypeso the properties of the date () object created by the new date () are inherited from Date.prototype and Object.prototype, The prototype of this series of links is the so-called " prototype chain ". such as:
var plo=object.create ({x:1, y:2});    // the PLO inherits attributes X and y var plo=object.create (null);    // creates a new object without a prototype, and PLO does not inherit any properties and methods, which means that he cannot work with the "+" operator var plo=object.create (object.prototype);    // Create an empty object, as with {}
  3. An object that is an associative arrayObject.prot//using the dot operator, a bit like C and Javaobject["prot"]//Use square brackets and a string that looks more like an array, but uses a string index, which is an associative array, or a hash, a map, or a dictionary When you access the properties of an object by [], the property name is represented by a string. Strings are JavaScript data types that can be modified and created when the program is run. such as:
var addr= "";  for (var i=0;i<4;i++) {    addr+=customer["address" +i]+ ' \ n '}
This code reads the properties of the customer-exclusive ADDRESS0,... ADDRESS3.   4. Simple InheritanceSuppose you want to query the property X of the object o, and if X is not present in O, the property X will continue to be queried in the prototype object of O. if there is no X in the prototype object, but the prototype object also has a prototype, then continue in the prototype object of the prototype object query, guidance to findx or to find an object with a prototype that is null. As you can see, the prototype property of an object forms a "chain" that enables inheritance of attributes through this "chain".
var o={}    //O inherits from Object.prototypeo.x=1; var p=o    //P inherits from O and Object.prototypep.x=2; alert (o.x)    // 2
  5. Detection Properties method One: In (can differentiate between non-existent attributes and properties that are present but have a value of undefined)
var o={    x:1}' x ' in     o    //true' y '    in o    //false"toString" in     o    //True:o Inherit the ToString property Delete  o.x    "x" in     o    //false: property is not present in the
  method Two: hasOwnProperty () is used to detect whether a given name is a free property of an object. Yes, returns true if the inherited property returns false.
var o={    x:1}o.hasownproperty ("x")    //trueo.hasownproperty (" Y ")    //falseo.hasownproperty (" toString ")    //false
  Method Three:!== (the operator can distinguish between undefined and null, and "! = Is not")
var o={    x:1}o.x!==1;    // false
 
              

JavaScript authoritative design--javascript object (Brief study note seven)

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.