Understanding the javascript_04 _ Data Model

Source: Internet
Author: User
Tags types of functions

This article describes the Javascript data model, which is a global overview of the data types supported by JavaScript.ArticleThe comparison is theoretically in-depth, so it is difficult to understand, but it must have an image for the data model because it is the basis for understanding the JavaScript Object Model and Javascript execution model.

 

Basic Data Types

Original Type (simple data type and basic data type)

Undefined type: indicates the value of a variable declared but not granted to it during initialization. Undefined is a unique value of the undefined type.

Null: indicates an object that does not exist. There is only one dedicated value of null type.

Boolean Type: it has two values: true and false, which are mainly used for condition judgment and control the execution process.

Number Type: indicates a number (including an integer of 32 and a 64-bit floating point number)

String type: used to represent a string.

NOTE: For the relationship between undefined and null, refer to the article "Understanding cript_02 _ understanding undefined and null.

 

Object:A set of unordered attributes whose values are simple data types, objects, or functions. Note: the object here does not refer to the Global object.

 

Function:A function is a type of object. The value of the internal attribute [[Class] is "function", indicating that it is a function type. Besides the internal attribute methods of the object, there are also internal attributes such as [construct], [Call], and [Scope. The processing mechanism of a function as a function call is different from that of the constructor (except for a function object). The internal method [[construct] is used to implement the logic as a constructor, method [[Call] implements the logic used as the function call. Same as above, the function here does not refer to the Global object function.

Note: The relationship between a function and an object can lead to many problems. Now, we can not go into the internal details of the function implementation, which will be discussed in future articles.

Note: The concept of "Basic Data Type" is different from that of "Basic Data Type". "Basic Data Type" refers to the most common data type, "Basic Data Type" refers to the original type (for more information about the original type and reference type, see the article "Understanding javascript_01 _ understanding memory allocation ).

 

Built-in data type (built-in object)

Function: user interface of the function type.
Object: user interface of the object type.
Boolean, number, and string: these three simple value type object wrapper. The object wrapper is similar to box/Unbox in C #/Java.
Date, array, Regexp: You can think of them as several built-in extended data types.

 

First, functions, objects, Boolean, number, String, date, array, and Regexp are all built-in objects in the Javascript language. They can all be considered as derived types of functions, for example, if number instanceof function is true and number instanceof object is true. In this sense, they can be treated like user-defined functions.
Second, they can represent a data type. The JS engine uses native code or built-in JSCodeImplementation is an interface exposed to developers to operate on these built-in data types. In this sense, they are all abstract concepts, and the specific implementation mechanism is hidden behind them.
In every place where words such as number and function are mentioned, they should be quickly instantiated as one of the two cases above in thinking.

 

Data Type Implementation Model Description

Note: images from http://www.cnblogs.com/riccc

Build-in ** Data Structure: this refers to the data structure used internally by JS to implement the ** type, provided by the host environment (browser). These structures cannot be operated directly.
Build-in *** object: it refers to the built-in number, String, Boolean and other objects in JS. This is the interface that JS exposes internal implemented data types to developers.
Build-in *** constructor: refers to some constructors built in JS to construct corresponding object instances. They are encapsulated as function objects and exposed. For example, we can access these function objects using the following method:

// Passed in ff2.0, IE7, opera9.25, safari3.0.4 // access the build-in number constructorvar number = new number (123); var numconstructor1 = number. constructor; // Orvar numconstructor2 = new object (123 ). constructor; // both numconstructor1 and numconstructor2 are the build-in number constructornumconstructor1 = numconstructor2 // result: True // access the build-in object constructorvar objconstructor1 = {}. constructor; // Orvar objconstructor2 = new object (). constructor; // both objconstructor1 and objconstructor2 are the build-in object constructorobjconstructor1 = objconstructor2 // result: True

An Explanation of "interface": Simply put, an interface is a method that can be called. For example:

 
// String is an interface that defines the behavior of string. it can be called externally by var str = new string ('dumps' motto '); // We define an interface function say (MSG) {alert (MSG );} // call the defined interface say ("Hello World ");

Note: To fully understand the concept of interfaces, you must have some strong programming experience (Java/C #). This article is already complex enough and will no longer complicate the problem. Therefore, the answer to the interface is not very rigorous, but it is enough. I am sorry.

 

Objectization of simple data types

This is a subtle place. The following description applies to the Boolean, string, and number types.
JS specification requirements: use VaR num1 = 123; such code directly returns the basic data type, that is, the returned object is not derived from the number and object type, and the value of num1 instanceof object is false; if the new keyword is used, the return value belongs to the number type. For example, VAR num2 = new number (123) and num2 instanceof number is true.
If number is used as a function call, the returned result is converted to a simple numeric type. The following is the test code:

 
// Passed in ff2.0, IE7, opera9.25, safari3.0.4var num1 = new number (123); // num1 derived from number & objectnum1 instanceof number // result: truenum1 instanceof object // result: true // convert the num1 from number type to primitive type, so it's no longer an instance of number or objectnum1 = Number (num1); num1 instanceof number // result: falsenum1 instanceof object // result: falsevar num2 = 123; // num2 is a primitive typenum2 instanceof number // result: falsenum2 instanceof object // result: false

Conclusion: Although we have obtained a simple numeric type, it still seems to be a JS object with all attributes and methods of the object and corresponding types. There is basically no difference in usage, the only difference is the test result of instanceof. Thus, a concept "literal Syntax" is created"

 

Literal syntax

In the object-oriented section of simple data types, we also see that simple types and their packaging types can be converted to each other, and the behavior between the two is the same. However, compared with the two, simple type definitions are much more lightweight, so we can replace the corresponding packaging type definitions with simple type definitions. For example:

 
Number: var I = 100; // replace var I = new number (100); Boolean: var B = true; // replace var B = new Boolean (true); string: vaR STR = 'this is a string. '; // replace var STR = new string ('this is a string ');

In fact, this definition is similar to VaR I = 100; var B = true; var STR = 'this is a string', which is called literal syntax. Is this literal syntax representation available only for simple data types! No. Composite data types are also available.

// The literal representation of the object definition var OBJ = {Name: 'dumps', age: 25}/* // non-literal expression of the object var OBJ = new object (); obj. name = 'dummies '; obj. age = 25; * // array-defined literal representation var arr = ['dumps', 25]; /* // array non-literal representation var arr = new array (); arr [0] = 'dummies ']; arr [1] = 25; * /// Regular Expression var Reg =/\ D + /; /* // Regular Expression var Reg = new Regexp ("\ D + ");*/

What about the function! In fact, the function definition is already in the form of literal syntax.

In practice, we recommend that you define variables in the form of literal syntax as much as possible, because this is simpler and more efficient.

 

Suggestions from "Dummies"

Although most of the articles are for reference, they also contain many of my experiences. Therefore, we provide suggestions:

1. For Beginners, You need to master the "Basic data type" and "literal Syntax" sections, and the other sections have an image.

2. The article is indeed very in-depth and does not require full understanding, because it involves many internal things, including the object model and execution model. These contents will be discussed in future blog posts.

 

Finally, I wish you all the gains and common progress.

 

Refer:

Http://www.cnblogs.com/RicCC/archive/2008/02/15/JavaScript-Object-Model-Execution-Model.htmlhttp://blog.csdn.net/yangdengfeng2003/archive/2007/01/20/1488513.aspx
 
 

 

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.