The essence of JavaScript language-notes

Source: Internet
Author: User

1. Object Property Retrieval

 
VaRStudent ={Name:"Zhan"};

To retrieve the name, use student ["name"] or student. Name.

2. Object Property update

Student. Name = 'wang ';

Student. Nickname = 'yu '; // if the object does not have the nickname Attribute before, it is automatically expanded to the modified object.

3. Object Reference

VaR x = student;

X. Nickname = 'Tom ';

VaR Nick = student. Nickname;

Because both X and student point to the reference of the same object, Nick is 'Tom '.

4. hasownproperty checks whether the object has properties. It does not check the prototype chain.

5. When you use for in to traverse all attribute names of an object, the enumeration process will list all attributes, including those in functions and prototypes.

VaR name;

For (name in student ){

};

6. Four function call modes:

 Method call mode: A function is called a method when it is saved as an object.

 
VaRMyobject ={Value:0; Inscrement:Function(INC ){This. Value + =TypeofINC = 'number '? INC: 1;}}; Myobject. increment (); document. writeln (myobject. value );//1Myobject. increment (2); Document. writeln (myobject. value );//3

function call mode : When a function is not an object attribute, it is called as a function.

 myobject.  double  =  function   () {  var  that =  This  ;   var  helper =  function   () {That. value  =  Add (that. value, that. value) ;}; helper ();  ///   call helper as a function  };  //   call double as a method  myobject.  double   (); w.net. writeln (myobject. getvalue ();  ///   6  

constructor call mode :

  //   Create a constructor function named quo. It constructs an object with the status attribute.   var  quo =  function   (string) {  This . status =  string ;};  ///   provides a public method named get_status for all quo instances.  quo. prototype. get_status =  function   () {  return   This  . status ;};  ///   construct a quo instance   var  myquo =  New  quo (" confused "); document. writeln (myquo. get_status ();  

Apply call mode: The apply method allows us to construct a parameter array and use it to call functions. It allows us to select the value of this. The apply method accepts two parameters. The first is the value to be bound to this, and the second is a parameter array.

 //  Construct an array containing 2 numbers and add them together  VaR Array = [3, 4 ];  VaR Sum = Add. Apply ( Null , Array ); //  The sum value is 7.  //  Construct an object that contains status members.  VaR Statusobject = {Status: 'A-OK'};  //  Statusobject does not inherit from Quo. prototype, but we can call the get_status Method on statusobject, although statusobject does not have a method named get_status.  VaR Status = Quo. Prototype. get_status.apply (statusobject );  //  The status value is A-OK 

7. Add methods to the type:

 

 

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.