Home>

This article is an intermediate advanced summary of javascript.We hope that everyone can read it and appreciate it in 30 minutes.Tap on the code,More summary, after all, javascript itself is not difficult.

Talk about javascript objects

javascript as a weak language type language,It is also a dynamically typed language.In the process of using javascript,It is also often necessary to use JavaScript's built-in objects and custom objects.

1.1 How to create objects

JavaScript is a weak language type language,It is not necessary to create objects through constructor methods like high-level languages ​​such as java and c#.In JavaScript, there are two main methods for creating objects.One is directly defined by the new keyword,One is defined by functions.as follows:

 //The first way,Create objects directly through new;
  var demo=new object ();
  demo.name="anderson";
  demo.sex="male";
  demo.age=23;
  //The second format,Instantiate directly,Basically consistent with json syntax,Generally used as an alternative syntax for the first way.
  var demo={name:"anderson", sex:"male", age:25};
  //The third format,Creating objects through constructors
  function demo (name, sex, age) {
    this.name=name;
    this.sex=sex;
    this.age=age;
  }
  var demo=new demo ("andeson", "male", 25);

1.2 How to modify objects

JavaScript and high-level languages ​​have different object syntax.In general,It has fewer restrictions,When the object is created,You can dynamically modify the properties and methods of objects,Such as adding a new attribute,Add a new method, etc.

 var demo={name:"anderson"};
  demo.sex="male";

1.3 Understanding Digital Objects in JavaScript

In javascript, all numbers are 64 bits, and all numbers consist of floating point types.In javascript, the 64-bit floating-point format defined by the ieee754 standard is used to represent numbers.It can represent a maximum of ± 1.7976931348623157 x 10308 and a minimum of ± 5 x 10 -324.

 var demo1=7;//decimal notation
  var demo2=07;//octal notation
  var demo3=0x12;//Hex notation
  var demo4=12e12;//scientific notation
  var demo5=12e-12;//scientific notation
  var demo7=128;
  var demo8=demo7.tostring (16);//Convert to Hex
  var demo9=demo7.tostring (8);//Convert to octal
  var demo10=demo7.tostring (2);//Convert to binary
  var demo11=infinity;//infinity representation
  var demo12=isnan (100);//non-numeric value representation
  var demo13=typeof (12);//The type of the number is number
  var demo14=typeof (new number (12));//number is of type object
  var demo15=(demo13 == demo14);//Both are equal
  var demo16=(demo13 === demo14);//The two are not equal

1.4 Understanding String Objects in JavaScript

 var str="hello, anderson";
  var demo1=str [0];//by index,Get the characters in a string,If it exceeds the maximum value, it returns undefined
  var demo2=str.indexof ("h");//Get the position of the target string in the source string,-1 if not found
  var demo3=str.length;//Get the length of the string
  var demo4=str.lastindexof ("h");//Get the position of the target string in the source string,Start looking at the end of the string,Not found returns -1
  var demo5=str.match ("anderson");//match the target string from the source string,If so, return the target string,Otherwise, returns null
  var demo8=str.replace ("a", "d");//Replace the destination string in the source string with a replacement string
  var demo9=str.touppercase ();//Convert to uppercase
  var demo10=str.tolowercase ();//convert to lower case
  var demo11=str.split ("d");//Convert to array
  var demo12="\" ";//means single quote
  var demo13="\" ";//means double quotes
  var demo14="\\";//means slash
  var demo15="\ n";//represents a newline
  var demo16="\ r";//Return
  var demo17="\ t";//represents a tab
  var demo18="\ b";//means space
  var demo19="\ f";

1.5 Understanding Date Objects in JavaScript

 var demo1=new date ();//Create an object,Current date
  var demo2=new date (22e9);//Create object,Milliseconds
  var demo3=new date ("2016-06-04");//Create an object,Date string
  var demo4=new date (2016,5,12,12,12,12);//create object,Year, month, day, hour, minute, second
  var demo5=(demo1>demo2);//compare dates

1.6 Understanding Array Objects in JavaScript

 var demo1=new array (1,2,3,4);//Create array
  var demo2=[1,3,4,5];//Create array
  var demo3=demo1.concat (demo2);//Merge array
  var demo5=demo1.concat (demo2, demo3);//Merge array

1.7 Understanding regular objects in JavaScript

regexpis an abbreviation for regular expression. Regular expressions are mainly used for text retrieval.Its basic syntax is as follows:

 var pattern=new regexp (pattern, modifiers);//Create regular object through constructor
  var pattern =/pattern/modifiers;//directly declare regular objects
  //Among them, there are two kinds of modifiers, i and g, i means case-insensitive,g means full text search
  //pattern indicates the retrieval model
  var str="hello, anderson lu";
  var pattern =/llo/gi;
  var demo1=str.match (pattern);//Usage example
  var pattern2=new regexp ("\\ lu \\ gi");
  var demo2=pattern2.test (str);//determine if there is a string in str that matches the pattern pattern2, return true or false
  var demo3=pattern2.exec (str);//Returns the matched string
  • Previous Graphical triangle relationship between prototype, prototype, and constructor
  • Next Bootstrap tutorial JS plugin popup study note sharing