Felayman-Summary of javascript skills

Source: Internet
Author: User

/*********************************** This the document content is updated for a long time ************************************ ***********/

1. Because the browser does not recommend repeatedly changing the Data Type of the same variable for parsing differences between different js Data Types

For example, var message = "felayman ";

The following operations are not recommended:

Message = 100;

Message = 99.9;

Message = false;

Messsage = null;

This will increase the browser resolution cost, and the number of changes is too large, which is not conducive to development.

Therefore, we recommend that you keep only one data type for variables.

Perform the following operations:

Var message = "hello ":

Message = "wolrd ":

Message = "helloworld ";

2. Pay attention to the scope when defining variables.

For example:

// Global variable

Function fun (){

Message = "hello": // global variable. The variable is not destroyed after the function is completed.

}

Fun ();

Alert (message); // The message is still exposed.

// Local variable

Function fun (){

Var message = "hello ";

}

Fun ();

Alert (); // nothing will pop up, because the variable is destroyed after the function ends.

3. variables are defined in the same way as c and java, that is, multiple variables are defined after a var keyword.

For example:

Var name1, name2, name3 ,....;

Or

Var name1 = "name1 ",

Name2 = "name2"

...................

Namen = "namen ";

4. Identify the six data types of js

@ 1 undefined: a variable is not assigned a value after it is defined.

For example:

Var message;

Alert (message = undefined); // true is displayed.

@ 2 NULL cannot be interpreted as ''. Generally, the value of a variable is null or a null object.

For example:

Var obj = null;

Alert (obj );

Or

Var obj = null;

Alert (typeof obj); // the object will pop up

@ 3 boolean this value indicates true or false

True:

1. true itself

Var bool = true;
If (bool)
Alert (bool );

2. Non-empty string

Var str = "hi ";

If (str)

Alert (str );

3. Non-zero number

Var str =-1;

If (str)

Alert (str );

4. Object (non-null object)

Var str = new String ("hello ");

If (str)

Alert (str );

5. Three methods for creating objects in js

@ 1: Create an object using a common object-oriented idea

Var obj1 = new Object ();

Obj1.name = "felayman ":

Obj1.age = 22;

Obj1.hober = "basketball ";

Alert (obj1.name + obj1.age + obj1.holobby );

@ 2: Create an object in JSON format

Var obj2 = {

Name: "felayman ",

Age: 23,

Holobby: "soccer"

};

Alert (obj2.name + obj2.age + obj2.holobby );

@ 3: Write this directly

Var obj = {}; // equivalent to var obj = new Object ();

Obj. name = "felayman ";

Alert (obj );

@ 4: Object reproducibility

Var obj3 = obj2;

Alert (obj3); // attributes are the same as those of obj2

The following is a typical case: for different parameters, different outputs are provided.

Function displayInfo (args ){
Var output = '';
If (typeof args. name = "string "){
Output + = "name:" + args. name + "\ n ";
}
If (typeof args. age = "number "){
Output + = "age:" + args. age + "\ n ";
}
Alert (output );
}
DisplayInfo ({
Name: "felayman ",
Age: 22
});
DisplayInfo ({
Name: "felay"
});

In this way, it is a bit similar to java. It is not a simple data type, but a complex object.

When the object is passed into the function parameters, we can operate on the properties of the parameter object. This is also the data

It is the best way to transfer a large amount of data through object encapsulation.

When getting object parameters, we can get their attributes in two ways.

@ 1: args ['name']

@ 2: args. name

Both methods can obtain the desired parameters ., General books will say there is no difference between the two methods, but there is a small difference

For example, when an attribute contains spaces, it can only be obtained through.

Var obj = {};
Obj ['first name'] = "felayman ";
Alert (obj ['first name']); // The felayman prompt is displayed.

Alert (obj. first name); // The error is returned.


 

 

 

 

 

 

 

 

 

 


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.