Understanding the JAVASCRIPT_04_ data Model "Go"

Source: Internet
Author: User
Tags types of functions

This article mainly describes the data model of JavaScript, which is a global overview of the data types supported by JavaScript. The theory of comparison is very deep and therefore difficult to understand, but it is important to have an image of the data model, because he is the basis for understanding the JavaScript object model and the JavaScript execution model.

Basic data types

Primitive type (simple data type, base data type)

Undefined type: Represents the value that is assigned to a variable when it is declared but not initialized. Undefined is the only value under the undefined type.

Null type: Used to represent an object that does not already exist. Only one private value is null under the null type.

Boolean type: There are two values, true and false, that are used primarily for conditional judgment and control of the execution process.

Number type: represents numbers (that is, integers that include 32, and 64-bit floating-point numbers)

String type: Used to represent a string.

Note: For the relationship between undefined and null, see the article Understanding javascript_02_ Understanding undefined and null.

object: A collection of unordered properties with values of simple data types, objects, or functions. Note: Objects in this case are not specified as global objects.

function: A function is a kind of object, the implementation of the internal property [[Class]] value is "function", indicating that it is a functional type, in addition to the object's internal property methods, but also [[[Construct]], [[Call]], [[Scope]] such as internal properties. The function is different from the constructor (which creates an instance object using the New keyword) as a function call (except for the function object), and the internal method [[[Construct]] is used to implement the logic as the constructor, and the method [[call]] implements the logic as a function call. As above, the function here is not specifically a global object function.

Note: There are a lot of questions about the relationship between functions and objects, and it is now possible not to delve into the internal details of the functions, which will be explored in future articles.

Note: "Basic data Type" is not the same as "basic data Type", "Basic data type" refers to the most commonly used data type, "basic data type" refers to the original type (the question about the original type and the reference type, see Understanding javascript_01_ Understanding memory Allocation) article).

Built-in data types (built-in objects)

Function: The user interface for the type of functions.
Object: The user interface of the objects type.
Boolean, number, String: The object wrapper for these three simple numeric types, the object wrapper is conceptually somewhat similar to the Box/unbox in C#/java.
Date, Array, REGEXP: You can think of them as several built-in extension data types.

First, function, object, Boolean, number, String, Date, Array, RegExp, and so on are all built-in objects of the JavaScript language, and they can all be thought of as derived types of functions, such as number instanceof function is True,number instanceof object is true. In this sense, they can be equated with user-defined functions.
Second, they can each represent a data type, which is implemented by the JS engine with native code or built-in JS code, and is an interface that exposes developers to these built-in data types. In this sense, they are all an abstract concept, which hides the concrete implementation mechanism behind it.
In every place where words such as number, function and so on are mentioned, they should be quickly instantiated into one of the two situations in the mind.

Data type Implementation Model description

Note: Images from HTTP://WWW.CNBLOGS.COM/RICCC

build-in * * * data structure: refers to JS internal for the implementation of the type of data structure, provided by the host environment (browser), these structures we basically can not directly operate.
Build-in * * * object: Refers to JS built-in number, String, Boolean and other such objects, this is JS will internally implemented data types exposed to the interface used by developers.
Build-in * * * constructor: Refers to JS built-in constructors, used to construct the corresponding type of object instances. They are packaged as function objects exposed, for example we can access these function objects using the following methods:

123456789101112 //Passed in FF2.0, IE7, Opera9.25, Safari3.0.4//access the build-in number constructorvarnumber = newNumber(123);varnumConstructor1 = number.constructor; //orvarnumConstructor2 = newObject(123).constructor;//both numConstructor1 and numConstructor2 are the build-in Number constructornumConstructor1 == numConstructor2 //result: true//access the build-in object constructorvarobjConstructor1 = {}.constructor; //orvarobjConstructor2 = newObject().constructor;//both objConstructor1 and objConstructor2 are the build-in Object constructorobjConstructor1==objConstructor2 //result: true

Explanation of "interface": Simply put, an interface is a method that can be called. Such as:

12345678 //string is an interface that defines the behavior of string. It can be called externally by var  str = new  string ( ' idiot's motto ' //We have a custom interface function  say (msg) {     alert (msg); //call defined interfaces Say (

Note: A full understanding of the concept of interfaces requires a certain amount of strong-type language programming experience (java/c#), because this article is complex enough to not complicate the problem. So the answer to the interface is not very rigorous, but already enough, looking at the expert forgive me.

About the object of a simple data type

This is a subtle place, described below for the Boolean, string, and number three simple numeric types that are applicable, as shown in number.
JS specification requires: use var num1=123; Such code, directly return the basic data type, that is, the returned object is not derived from number and object type, with Num1 instanceof object test to False , created with the new keyword, returns the number type, such as var num2=new number (123); Num2 instanceof number is true.
The number is treated as a function call, and the returned result is converted to a simple numeric type. Here is the test code:

1234567891011 //Passed in FF2.0, IE7, Opera9.25, Safari3.0.4varnum1 = newNumber(123); //num1 derived from Number & Objectnum1 instanceofNumber //result: truenum1 instanceofObject //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 instanceofObject //result: falsevarnum2 = 123; //num2 is a primitive typenum2 instanceofNumber //result: falsenum2 instanceof Object //result: false

Conclusion: Although we get a simple numeric type, it still looks like a JS object, with object and all the properties and methods of the corresponding type, with no difference in usage, and the only difference is the instanceof test results. Thus, a concept "Literal Syntax" is produced.

Literal Syntax

In the object-only section of simple data types, we also see that simple types and their wrapper types can be converted to each other and behave the same. However, the definition of a simple type is lighter, so we can replace the corresponding wrapper type definition with a simple type definition. Such as:

123 number: var   i = 100; //replaces var i = new number (+); boolean: var   b = true / /replace var B = new Boolean (true); string: var   str = " is a string. ' //instead of var str = new String (' This is a string ');

In fact, this kind of definition is called literal Syntax, which is similar to var i = 100;var B=true;var str= ' This is a string '. Is it only simple data types that have this literal syntax representation method? No, the composite data type has the same.

123456789101112131415161718192021222324 //对象定义的字面量表示法varobj  = {name:‘笨蛋的座右铭‘,age:25}/*//对象的非字面量表示法var obj = new Object();obj.name = ‘笨蛋的座右铭‘;obj.age = 25;*/ //数组定义的字面量表示法vararr = [‘笨蛋的座右铭‘,25];/*//数组的非字面量表示法var arr = new Array();arr[0]=‘笨蛋的座右铭‘];arr[1]=25;*///正则表达式字面量表式法varreg = /\d+/;/*//正则表达式非字面量表式法var reg = new RegExp("\\d+");*/

What about the Function! In fact, the definition of a function is already a representation of literal syntax.

In practice, we recommend that variables be defined in the form of literal syntax as much as possible, because it is simpler and more efficient.

From "idiot" advice.

Although most of the article is reference, but it also contains a lot of my experience, so give advice:

1. For beginners, it is necessary to master the "basic data Type" and "Literal Syntax" section, the other parts have an image.

2. The article is really deep and does not require complete mastery because it involves a lot of things, including the object model and the execution model. These contents will be discussed in the future blog post.

Finally, I wish you a fruitful and common progress.

Reference:

123 <a href="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">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</a>
1

Understanding the JAVASCRIPT_04_ data Model "Go"

Related Article

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.