A summary of JavaScript learning (i)--javascript Basics _javascript Skills

Source: Internet
Author: User

The features of JavaScript language

1.1, JavaScript is based on object and event-driven (dynamic)

It can respond directly to user or customer input without having to go through a Web service program. Its response to the user is done in an event-driven manner. Event-driven refers to the action that is produced by performing an operation on the home page, called an "event". For example, press the mouse, move the window, select the menu, etc. can be considered as an event. When an event occurs, the corresponding event response may be caused.

1.2. JavaScript is Cross-platform

JavaScript is dependent on the browser itself, regardless of the operating system.

Second, JavaScript variables

2.1. Define variables

When defining a variable, the uniform uses the "var variable name" representation, for example: Var str; You can even omit the keyword var

2.2. How does the type of JavaScript variable determine

The data type of a variable in JavaScript is determined by the JS engine

 var name= "aloof Wolf";//name is a string type var
 age=;//age is the number type var
 flag=true;//flag is a Boolean type
 var email;// Email is just a declaration, no assignment, so the type of representative is "undefined", which means that
 name=;//name automatically becomes number type.

2.3, using the TypeOf keyword to view the specific data types of variables represented

The typeof operator has a parameter, which is the variable or value to check. For example:

 var stemp = "Test string";
 Alert (typeof stemp);  Output "string"
 alert (typeof);  Output "Number"

Calling the TypeOf operator on a variable or value returns one of the following values:

Undefined-If the variable is undefined type

Boolean-If the variable is a Boolean type

Number-If the variable is of type number

String-If the variable is of type string

Object-If the variable is a reference type or a Null type

Test code:

<script type= "Text/javascript" >
   var name= "aloof Wolf";//name is a string type
   alert ("Name is +typeof name+" type);
   var age=;//age is number type
   alert ("Age is" +typeof age+ "type");
   var Flag=true;//flag is a Boolean type
   alert ("Flag type is:" +typeof flag);
   Name=;//name automatically becomes number type
   alert ("After the name variable is assigned a value, the name's data type becomes:" +typeof name);
   var email;//email is only declared, does not assign a value, so the representative type is "undefined", that is, unable to determine
   alert ("Email type is:" +typeof email);
   var a=null;
 /*
 Why the typeof operator returns "Object" for a null value.
 This is actually an error in the initial implementation of JavaScript and is then used by ECMAScript. Now, NULL is considered a placeholder for an object, which explains the paradox, but technically it is still the original value.
 *
   /alert ("A is" +typeof A + "type");
  </script>

Run Result:

Three, JavaScript data types

  

JavaScript consists of two different data types: basic data types and reference data types. The basic type refers to simple data, and a reference type is an object consisting of multiple values. When we assign a value to a variable, the first thing the parser does is confirm whether the value is a base type or a reference type value.

3.1. Basic data type

  Common five basic types of data:

Boolean
Number
String
undifined
Null

These five basic data types can directly manipulate the actual values saved in the variable.

3.1.1, numeric type (number), and Boolean type (Boolean)

Look at the following code:

 <script type= "Text/javascript" >
   var a =;
   var b = A;
     b =;
   Alert ("a=" +a);//print a=
   
   var B = true;
   var B = b;
     b = false;
   Alert ("b=" +b);//print b=true
 </script>

Run Result:

From the results of the operation, it can be seen that the value of B is a copy of a value, although the value of two variables is equal, but two variables save two different basic data type values. b just saved a copy of a copy. So, when the value of B changes to 20 o'clock, the value of a is still 10. The two Boolean variables B1 and b2 are also basic data types, which also hold two different base data type values, b2 save a copy of 1 copies. So, when the value of the B2 changes to False, the value of the B1 is still true.

The following illustration shows the process of assigning this basic data type:

Stack memory

3.1.2, string-type (String)

String strings in JavaScript a particular basic data type, in many languages, string is represented as an object, but in JavaScript, string is treated as a basic data type, and is passed in a way that passes through values. But it is a very special basic type.

Look at the following example:

<script type= "Text/javascript" >
   var stra = "This is a string";
   var strb = stra;
     Stra = "This is another string";
     Alert ("strb=" +STRB);
 </script>

Run Result:

  

From the results of the operation can be seen, as if the stra through the value of the way to reproduce a copy to the STRB. When the stra changed, the STRB did not change, as if we had been able to conclude that string was a basic data type.

However, because a string can be arbitrary in length and passed by value, one copy byte display efficiency remains low, and it seems that a string can also be used as a reference type.

Look at the following example:

 var a = "MyObject";
 A.name = "MyName"; Add the name Attribute
 alert ("A.name=" +a.name) to string a dynamically;//access the Name property of a,

Results show:a.name=undefined

Run Result:

  

The run result shows that string cannot be treated as an object. This also proves a point: Although the basic type can also add attributes, no error, after the test has been added is inaccessible, in fact, the JavaScript string can not be changed, JavaScript does not provide any change in the string of methods and syntax.

Look at the following example:

 var b = "MyObject";
 b = b.substring (,);
 Alert ("b=" +b); B=bj

Run Result:

  

Do this, and there is no change string string "MyObject", only B refers to another string "BJ", "MyObject" is recycled.

So you can say that string does not actually fit the above two data type classifications. It is a special type with two properties between them.

3.1.3, Null type

The null type has only one private value null, and the value undefined is actually derived from the value null, so ECMAScript defines them as equal.

  <script type= "Text/javascript" >
    alert (the result of "NULL = = undefined is:" + (null = = undefined));/output "true"
  </ Script>

Run Result:

  

Although the two values are equal, they have different meanings. Undefined is the value that is given to a variable when it is declared but not initialized, and null is used to indicate an object that does not exist (the TypeOf operator returns "Object" for a null value). )。 If a function or method returns an object, the object is usually returned null when it is not found.

3.1.4, Undefined type

The Undefined type has only one value, that is, Undefined. When a declared variable is uninitialized, the default value of the variable is undefined.

var otemp;

The previous line of code declares the variable otemp, with no initial value. The variable is assigned the value undefined, that is, the literal amount of the undefined type. You can use the following code snippet to test whether the value of the variable equals undefined:

 <script type= "Text/javascript" >
   var otemp;
   Alert (The result of "otemp = = undefined is:" + (otemp = = undefined));/output "true"
  </script>

Run Result:

  

The run result displays "True", indicating that the two values are indeed equal.

You can use the TypeOf operator to show that the data type represented by the variable is undefined type

 <script type= "Text/javascript" >
   var otemp;
   Alert ("typeof otemp The result is:" +typeof otemp); Output "undefined"
  </script>

The value is undefined and differs from the undefined value. However, the typeof operator does not really distinguish between these two values. Consider the following code:

 <script type= "Text/javascript" >
   var otemp;
   Alert ("Otemp variable has a statement, the result of TypeOf Otemp is:" +typeof otemp); Output "undefined"
   alert ("Otemp variable is not declared, typeof otemp result is:" +typeof otemp);/output "undefined"
  </script>

Run Result:

The output of two variables is "undefined", even if only the variable OTEMP2 has never been declared. If you use an operator other than typeof for OTEMP2, it can cause an error because the other operators can only be used on declared variables.

The following code throws an error:

var otemp;
 Alert (otemp = = undefined);//' otemp ' not defined

When a function does not explicitly return a value, it returns the value "undefined" as follows:

function TestFunc () { 
   //This is an empty function, no return value
 }
 alert ("testfunc () = = undefined result is:" + (testfunc () = = undefined) ); Output "true"

Run Result:

  

3.2. Reference data type

JavaScript reference data types are objects that are stored in heap memory, and JavaScript does not allow direct access to the location in the heap memory space and the operating heap memory space, only by manipulating the reference address of the object in stack memory. So the reference type of data, stored in the stack memory is actually the object in the heap memory reference address. This reference address allows you to quickly find objects that are saved in heap memory.

Look at the following example:

 <script type= "Text/javascript" >
   var obj = new Object ();
   var obj = obj;
   Obj.name = "Aloof wolf";
   alert (obj.name); Lonely Wolf
 </script>

Run Result:

  

From the above example, we declare a reference to the data type variable obj1 and assign it to another reference data type variable OBJ2. When we obj2 added a name attribute and assigned "aloof Wolf". OBJ1 also has the same name property as Obj2. Description The two reference data type variables point to the same object. The obj1 assignment to Obj2 is actually a copy of the object's reference address in the stack memory to the OBJ2, but they essentially point to the same object in the heap memory.

Let's demonstrate this reference data type assignment procedure

    

Naturally, adding the name attribute to the OBJ2 is actually adding the name attribute to the object in the heap memory, and OBJ2 and obj1 are storing only the reference addresses of the heap memory objects in the stack memory, although they are copied, but the same object is pointed to. Therefore, the change of OBJ2 caused obj1 change.

In general, the base data type is composed of a fixed number of bytes that can be manipulated at the lower level of the parser, such as number and Boolean, whereas reference data types can contain as many properties and elements as possible, so they cannot operate as easily as the basic data type.

Since the value of the reference data type changes, it makes little sense to pass the same value as the basic data type, because it involves a lot of memory duplication and comparison, and is inefficient. So the reference data type is passed by reference, which actually passes only one address of the object.

such as array and function, because they are special objects so they are all reference types. In addition, a reference type is a property that can be added, although the base type can also add attributes and do not have an error, but is not accessible after the test has been added.

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.