JavaScript Basic Section 2

Source: Internet
Author: User
Tags switch case

1.if judgment

1) If judgment is basically the same as Java

2) three structure if () {}; if () {}else{}; if () {}esleif () {}esle

2 for Loop, switch case, default;

1) Basic with Java

3 objects

3.1 Method 1:

1) object creation var Obj=new object ();

2) Add attribute obj.name= "Zhang San" for the object;

3) Invocation of object Properties Console.log (Obj.name);

3.2 Method 2:

1) object creation var Obj=new object ();

2) Add an attribute to the object obj["name"]= "John Doe";

3) object property derived call Console.log (obj["name"])

4) for 2) add var n= "name" later; Then change 3) to Console.log (Obj[n]) so that more flexible

3.3 Method 3:

1) object creation: var obj={name: "John Doe", age:18} Note that the following is a comma and there is no sign behind the last attribute;

2) Invocation of object properties: Console.log (Obj.name) This method is more commonly used

The 3.4 property can be object: var obj={name: "John Doe", Obj1:{name: "Harry"}} console.log (Obj.obj1.name), the calling method is somewhat similar to the inner class in Java

4 functions

4.1 Method 1:

1) function Creation var fun=new function ("Stay away");

2) function creation Console.log (fun);

4.2 Method 2:

1) Create function fun () {console.log ("Haha! Oh, hello! ”)}

2) function call Fun ();

4.3 Method 3:

1) function creation var obj={name: "Zhang San", age:18} var fun=function (obj) {console.log (obj.name+obj.age)};

2) Call Fun (obj) of the function method;

4.4 Use of anonymous functions

1) Assignment variable var a= function () {}

2) use as a static code block similar to Java (function () {})

4.5 object in the for (var n in obj) is similar to a foreach loop traversal in Java

1) Usage var obj={name: "Zhang San", Age:23,gender: "Male"}; for (var n in obj) {

Console.log (Obj[n])} with description: [n] indicates that the variable assigns the corresponding value 323 male to "n"

4.6 Generally do not define the corresponding function in the global scope this pollutes the global scope, can be defined on its prototype, so that the corresponding global scope of the pollution problem can be resolved as follows:

1) Create a function func () {}

2) Create a function instance var fun=new func ();

3) Set the prototype attribute Func.prototype.name= "Zhang San";

4) Instance invocation property Console.log (Fun.name); Description: Now look for itself, if not found, and then go back to the prototype to find

5) Find an instance of the prototype method __proto__ are 2 _; always find null when you can stop

6) Check if there is a property in the MC object Console ("name" in MC) This method if not found in itself will be in the prototype to find

7) Check if the MC object itself contains a property console (Mc.hasownproperty

("name") This method returns true only if it contains the corresponding property in itself

8) The introduction of the method tostring null and undefined cannot be used with the ToString method the rest of the usage is similar to Java's ToString can also be overridden by the ToString method, similar to Java, the example is as follows

var obj=new person ("Zhang San", "All"); Obj.tostring=function () {

return this.name+this.age;} Console.log (obj);

9) Call (): Invoke function execution such as Var fun=new function () {};fun.call ()

The object of this can be specified in (), or it can be written as call (object, parameter 1, Parameter 2), object to this, parameter 1, parameter 2 as the function's formal parameter

Apply (): Invokes the execution of the function, such as Var fun=new function () {};fun.apply ()

Apply () No follow-up method for Call ()

5 This

1) When a function is called in a direct global scope, this is the window;

2) When the function is not called directly in the scope, then this point points to the highest level of the object (except for the top layer of the window), so you can understand who is calling

6 arrays

6.1 Creation of an array in Call Method 1

1) Create object var arr=new array ();

2) add element arr "0" =1;arr[1]= "Hello";

3) Array call Console.log (Arr[0]);

6.2 Methods for creating and invoking arrays 2

1) Create and add elements var arr=[1,2,3,4];

2) Array call Console.log (Arr[0]);

6.3 Some common methods of arrays

1) length similar to Java

2) push () is similar to the trailing element of the linked list, and multiple Arr.push can be added at once

3) pop () Delete end element, call delete one element at a time

4) Unshift This is starting from scratch other similar push ()

5) Shift This is starting from scratch other similar POPs ()

6) Slice () similar to Java substring but different, slice can specify a negative number-1 means the countdown to the first-2 means the penultimate

7) sort () Java-like Collections.sort implements the comparable interface in a natural sort similar to Java

8) Join () joins the array elements into a string, and the connector can be added in the () Jion ("connector")

9) reverse () the corresponding inversion of elements in an array similar to the Collections.reverse in Java

Splice () specifies the number of reserved bits, default does not write all reserved, delete subsequent elements splice (n)

Concat () combines two numbers into a new array where you can put multiple arrays at once

6.4 Traversal of an array

1) Java-like traversal

6.5 JS arrays are similar to Java's mutable argument lists, unlike Java arrays

6.6 Array.isarray (object): Determines whether the object is an array object

6.7 Arguments is a class array object that stores the formal parameters of a function in an array as follows

Function Fun (a,b,c) {Console.log (Arguments[index])} index points to a,b,c to derive formal parameters, zero-based, callee refers to the currently executing object

7th Issue Date

7.1 How to create a date

1) Create object var date=new date (); Point to the current time Java-like date

7.2 Common methods

1) getday () says today is the day of the week

2) GetMonth () returns the month 0 for January

3) getFullYear () gets the current year

4) long gets timestamp

8 Packing class

1) wrapper class string () can be converted to a string object, most of the methods are similar to Java, but there are no strict order requirements for the two digits before and after substring

2) Number () converts the corresponding object to the number () object

3) Boolean () converted to Booelan () object

9 Regular Expressions

9.1 How to create a regular expression 1

1) Create regular match var reg=new regexp ("a");

2) Create the corresponding string object var str= "Asfsdf";

3) match var result=reg.test (str) existence returns true otherwise returns false

9.2 Common ways of regular expressions 2

Var str= "QQQQDSDQQQQQ";

Var Bl=str.match (/q{2,}/);

Console.log (BL);

Intercept the string that matches the regular, but only match the first matched string, need to match all of the following to add a/g the above modified var bl=str.match (/q{2,}/g), return an array, If it is not case-sensitive, then the corresponding modification is var bl=str.match (/q{2,}/i), the size is not differentiated and all match, then it needs to be modified to Var bl= str.match (/q{2,}/ig), the rest is similar to Java, Replace differs from Java's replace, replace () replaces only the first one, replaces all and is case-insensitive and is similar to match

JavaScript Basic Section 2

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.