JS Object-Oriented notes

Source: Internet
Author: User

0, the anonymous function will be called directly after: (Function (NUMA, NumB) {alert (Numa + NumB);}) (3,4);//pop-up windows 7

1, JS function is the object, the object is a function. Arguments represents a collection of parameters for a function

2, JS method directly called the function, with the new call as an object. There is no class syntax in JavaScript, which is simulated with a function closure.

3, JS in the dynamic addition of properties to the object example:

//Define a functionfunctionPerson () {
}
//1, call the function name directly, just simply represent the calling functionPerson ();//2, is to use new, said to create an object, JS is a dynamic language, you can dynamically add properties and methods to the objectvarper =NewPerson ();p er.name= "Xiao Ming";//adding properties to the Per object dynamically namePer.age = 26;//dynamically adding attributes to the Per object agePer.sayhi =function() {alert ("Hello");}//Adding a method to the per object dynamically Sayhi//to invoke the properties and methods of the Per objectalert (per.name);//or alert (per[' name ');Per.sayhi ();

4, JS support the This keyword, through the This keyword for the properties of the object to assign values

 function   person (name, age) { this . name = name; //      add the Name property to person and assign the value of the parameter name to the property      this . Age =< span> this . Sayhi = function  () {alert (" My name is "+ this . Name +", I ' m old "+ this  .age); }}  //  call   per = new  person ("xiaoming", 26);p Er.sayhi ( ); //  pop Windows ..... 

The function person (name, age) can be seen as declaring a constructor, and the properties of name and age are also dynamically added, and new is equivalent to creating an instance of the function

5. String Object Method Learning

    • Length property: Gets the number of characters in a string (1 characters in both Chinese and English characters)
    • CharAt (Index) method: Gets the character at the specified index position (index starting at 0)
    • IndexOf (' string ', StartIndex) method: Gets the position of the first occurrence in the specified string, StartIndex indicates the search from the beginning
    • Split (' delimiter ', limit): Returns a string as an array based on the delimiter. Limit indicates the maximum length of the array to be returned (customizable)
    • substr (StartIndex, Len): Starts from StartIndex, intercepts Len characters
    • SUBSTRING (StartIndex, Stopindex): From StartIndex, intercept to Stopindex position (excluding character of Stopindex)
    • toUpperCase (): Convert uppercase
    • toLowerCase (): Convert lowercase
    • Match (), replace (), search () method and regular expression
    • .....

6, Array object: JS in the Array object is arrays, is a dynamic array, dynamic add

Example: var names = new Array ();

Names[0] = "Zhang";

NAMES[1] = "Mao";

NAMES[2] = "Jiang";

7. Extension method: Set extension method through prototype of class object

 //  Extend a method Addsign (sign) to a string object;  String.prototype.addSign = function  if  (!sign) { If sign is empty  sign = "^_^" < span> return  sign + this  + sign;}  //  call test   var  str = "Tan" ;alert (Str.addsign ());  //  popup content: ^_^ Tan ^_^  alert ( Str.addsign ("* * *")); //  popup content: ***tan***  

JS Object-Oriented notes

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.