Basic concepts of JavaScript kernel (1)

Source: Internet
Author: User
Tags integer numbers

This chapter describes the data types (Basic Types and reference types), variables (including the scope of variables), and operators (mainly common, but not operators that can be understood literally ). Because "Everything is an object" in JavaScript, after understanding these basic concepts, readers can easily understand such as scope, calling objects, and closures, currying and so on.

Data Type

Readers who have experience in programming must know that in a language such as C or Java, data is of a type. For example, the attribute used to indicate the user name is a string, an employee's age is a number, indicating that the data model of a switch button on the UI is a Boolean value, etc. The numbers may be subdivided into floating-point numbers and integer numbers, integer types can be further divided into Long and Short integer types. In short, they all represent the types of data values in the language.

There are two types of data in JavaScript: Basic Data Type and object type. The object type includes objects, arrays, and functions (in fact, functions and arrays are also objects, this is detailed in later chapters ).

1.1.1 Basic Data Types

JavaScript contains three basic data types: String, Number, and boolean. Below are some simple examples:

 
 
  1. <Strong> var str = "Hello, world"; // string
  2. Var I = 10; // integer
  3. Var f = 2.3; // floating point number
  4. Var B = true; // Boolean value </strong>

We can view the values and types of variables respectively:

 
 
  1. print(str);  
  2. print(i);  
  3. print(f);  
  4. print(b);  
  5.    
  6. print(typeof str);  
  7. print(typeof i);  
  8. print(typeof f);  
  9. print(typeof b); 

Note that the print () function used here is the method of the top-level object of the rhino interpreter, which can be used to print strings. Generally, on the client, programmers usually use alert () for similar actions, alert () is a method of the top-level object (window) of the JavaScript interpreter in the browser.

 
 
  1. Hello, world  
  2. 10  
  3. 2.3  
  4. true 
  5.    
  6. string  
  7. number  
  8. number  
  9. Boolean 

In JavaScript, all numbers, whether integer floating point, belong to the basic type of "number. Typeof is a one-dimensional operator, which is discussed in another section in this chapter.

1.1.2 object type

The object mentioned here is not an object, but a type. In chapter 3, we will discuss the object in detail. The object here includes the object (set of attributes, hash of key values, arrays (ordered lists), and functions (including executable code ).

An object type is a composite data type. Its basic elements consist of basic data types. Of course, they are not limited to basic types. For example, the value of an object type can be an instance of another object type, we use examples to illustrate:

 
 
  1. <strong>var str = "Hello, world";  
  2. var obj = new Object();  
  3. obj.str = str;  
  4. obj.num = 2.3;  
  5.    
  6. var array = new Array("foo", "bar", "zoo");  
  7.    
  8. var func = function(){  
  9.     print("I am a function here");  
  10. }</strong> 

As you can see, an object has attributes, such as obj. str and obj. num. The values of these attributes can be basic types. In fact, they can be more complex. Let's take a look at their types:

 
 
  1. Print (typeof obj );
  2. Print (typeof array );
  3. Print (typeof func );
  4. // Print the output
  5. Object
  6. Object
  7. Function

Readers may wonder how print (typeof array) Prints objects. In fact, the boundaries between objects and arrays are not that obvious (in fact they belong to the same type ), however, their behavior is very different. Two important data types are introduced in the subsequent sections of this book.

2.1.3 conversion between the two

Similar to the automatic packing and unpacking of basic data types in Java, JavaScript also has similar actions. When some operations are performed on basic data types, an object is temporarily packaged. After the calculation, and Automatically releases the object. Here are several examples to illustrate:

 
 
  1. <Strong> var str = "JavaScript Kernal ";
  2. Print (str. length); // print 17 </strong>

Str is a string. The typeof operator indicates that its type is "string", while:

 
 
  1. <strong>var str2 = new String("JavaScript Kernal");  
  2. print(typeof str2);</strong> 

We can see that the type of str2 is "object", that is, the two are not the same. Why can we use str. length to get the length of str? In fact, when str. during length, JavaScript automatically wraps a temporary String object whose content is str, obtains the length attribute of the object, and then releases the temporary object.

To convert an object to a basic type, you can call the valueOf () method of the object to obtain the value of the object. If the value matches the context type, the value is used. If valueOf cannot obtain the value, you need to call the toString () method of the object. If the context is numeric, You need to convert the string to a value. Because JavaScript is of a weak type, the JavaScript engine needs to "Guess" the object type based on context, which makes JavaScript more efficient than compiled languages.

ValueOf () is used to convert the value of an object into a basic type that meets the context requirements. toString () is a real name and can print the corresponding string of the object, of course, the premise is that you have "overloaded" The toString () method of the Object.

In fact, this conversion rule will cause many problems. For example, all non-empty objects will be converted to true in a Boolean environment, for example:

 
 
  1. <strong>function convertTest(){  
  2. if(new Boolean(false) && new Object() &&  
  3.  new String("") && new Array()){  
  4.        print("convert to boolean")  
  5.     }    
  6. }  
  7.    
  8. convertTest();//convert to Boolean</strong> 

Beginners are easily confused by the type conversion rules in JavaScript.In many cases, you may find the writing method quite awkward. In fact, you only need to master the rules. These odd writing methods will greatly improve the Code performance. We can use examples to learn these rules:

 
 
  1. <strong>var x = 3;  
  2. var y = x + "2";// => 32  
  3. var z = x + 2;// => 5  
  4.    
  5. print(y);  
  6. print(z);</strong> 

The following code can be found in JS Code:

 
 
  1. <strong>if(datamodel.item){  
  2.     //do something...  
  3. }else{  
  4.     datamodel.item = new Item();  
  5. }</strong> 

This writing actually has a deeper meaning:

It should be noted that datamodel. item is an object (string, number, etc.), and if requires a boolean expression, so type conversion is performed here. In JavaScript, if the context requires a boolean value, the engine automatically converts the object to a boolean type. The conversion rule is: if the object is not empty, convert it to true; otherwise, false. Therefore, we can adopt this short form.

In traditional programming languages (strong types), we need:

 
 
  1. <strong>if(datamodel.item != null){  
  2.     //do something...  
  3. }else{  
  4.     datamodel.item = new Item();  
  5. }</strong> 


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.