Let's talk about the JavaScript data type.

Source: Internet
Author: User

The reason for this discussion is that, in my MSN blog (now wordproess, here), The argb team replied, the earliest reason was a big discussion about JavaScript journey two years ago:

Starting from "loading B As a hacker"

-- Let it go. During the discussion, I mentioned the chaos in the book's introduction to JavaScript-type systems, and argb turned out this historical article, referring to my confusion. So I listed the following questions for him:

I want to explain your problem very quickly. Then, let me ask you a few questions:

1. Is the function type? What type is it?

2. Why is the function in JavaScript the first type?

3. How does undefined package into an object "? Even if you are talking about a clerical error, what is undefined for JavaScript that "Everything is an object?

4. What are the differences between true and Boolean (true) in type?

Finally, let's emphasize your wording: Undefined is a type, undefined is a value, and 'undefined' is a type name. In addition, pay attention to the value type and reference type in JavaScript.

Then, the reply from argb made me feel that everything had to be done. Because we haven't discussed the type system issues in the JavaScript journey before, so I just need to complete my homework. Let's talk about it seriously and reply to argb's students by the way.

Thanks again to argb. Otherwise, I still have to owe a long time for this assignment. It is always good to have the interest of readers and friends. Q & A explanation is a matter of beauty.

Let's talk about the JavaScript data type.

First, let's talk about two things. First, JavaScript is not a pure object-oriented language, but a hybrid language. Therefore, the so-called "all object-oriented" is both a propaganda term and a language processing technique. Many mistakes will be made to understand the type of the language only from "Object-Oriented. Second, the description of ECMAScript is always accurate and lags behind the development of this language. Therefore, we need to understand some phenomena, both from the history of JavaScript and the current development of JavaScript. ECMAScirpt is a standard and standardized reference, but not all.

Next we will talk about the type. JavaScript is both a procedural language and an object-oriented language. To some extent, it also shows that it actually has two types of systems. The first type system is identified by typeof, which is the basic type system of the language. There are only six types, namely undefined, number, boolean, string, object, and function. I generally call it a basic type system. The reason for this is "Basic" because the second type system is based on it and has been developed from the object type system, that is, the object type system.

The object type system uses instanceof for identification. It is equivalent to the is Operation/operation in other advanced languages. Object-oriented polymorphism is mainly expressed by as and is. For JavaScript, as it is weak (there is no forced type check), as is not required.

There is a ing relationship between the object type system and the basic type system. For example, the basic type string is mapped to the String in the object system. However, this is only an shadow, so in essence, the string type is not a String type. The two are essentially different. Specifically, undefined, string, number, and boolean are "value types", and object and function are "reference types ". Because String, Number, and Boolean belong to the object type in the basic type and are subclasses of Object (), they are reference types. Function () is also a reference type. All reference types can look at the subclass of Object (), so any function is also a subclass of Object. For example" <匿名函数> Instanceof Object "returns true.

Undefined is a value type, and it does not have the corresponding object type-we can usually call it the Undefined type, but it does not have the corresponding constructor. Undefined has only one value, that is, undefined. To be accurate, undefined indicates a variable that has been declared (or generated) but has no value. Null is also a type, and null is its unique value (according to the language rules, null is also a keyword ). The Null type is the object type, that is, the reference type. Therefore, Null and Undefined are essentially different because they belong to different types of systems and explain the concept of "none" in different types of systems. In general, null should be used if no value exists for an attribute or member in the DOM, and undefined should be used if no value exists during JavaScript operations.

The above emphasizes that the above types should be understood from the perspective of "two types of systems. These two types of systems can be mixed in JavaScript. The technology that implements this feature is called "Class packaging ". This is the main reference of JavaScript For Java and also one of the major references of. NET for Java. Class packaging is also known as "Packing" (and "unpacking "). The class Packaging Process in JavaScript appears in the access to natural attributes, that is, ". Operator" or "[] operator ". When the two operators find that the left operand x is a "Value Type" data, they will implicitly call the Object (x) process to convert it into an Object. Therefore

'Abc'. length

This operation is actually equivalent

Object ('abc'). length

Finally, let's go back to the original issue. So I said:

There are six basic types in JavaScript, one of which is the object type. Various objects are subclasses (types) of the object type ).

There are no errors. In his book, Mr. Zhu said:

-JavaScript has only three primitive data types: numeric, string, and Boolean.

-JavaScript also defines several special data types, such as null and undefined ).

-Basic data types are transmitted by value, while complex data types are transmitted by reference.

These opinions are not reliable. First, these three are the original data types, but they are not "only three". I will talk about them later. Second, the Null type and Undefined type are both incorrect. They should be Null and Undefined type-lower case, which is their value; the first letter is their type. Thirdly, undefined is passed by value. However, in Mr. Zhu's classification, we do not know how to belong. He mentioned at least: raw data type, special data type, value (passed) type, reference (passed) type. Such a complex classification will make readers more confused.

Finally, let's talk about the "original data type ". This term is called "primitive types" in ECMAScript, but this concept is derived mainly from "primitive values, instead of being used as the basis for a type classification, the primitive type is mentioned only once in ECMAScript and is not called "types ". ECMAScript uses "primitive values" to describe the original values of some Types. For example, Boolean Types has the original values true/false. However, this does not indicate the difference or relationship between the Boolean object type and the value type. For example, it cannot indicate the difference between true and Boolean (true.

ECMAScript uses "primitive values" and states the definitions of these original values, mainly because ECMAScript must take into account the implementation scheme of the JavaScript language. A considerable part of ECMAScript is describing the implementation of a language. In many cases, an object needs to be converted to "primitive values ", or use the term "primitive values" to describe its actual implementation-but I must stress that this has nothing to do with the definition and planning of the type system. For example, when ECMA describes the concept of "property", the original article is:

"Properties are containers that hold other objects, primitive values, or functions. A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, and String; an object is a member of the remaining built-in type Object; and a function is a callable object. A function that is associated with an object via a property is a method."

The translation is:

Attributes can include other objects, original values, or functions. A primitive value is a member (that is, a value) of the following built-in types: Undefined, Null, Boolean, Number, and String; an object) is a member (instance, instance) of other built-in object types, and a function is a callable object. If a function acts as an object property, it is called a method ).

What is the relationship between the above description and "how to divide a type system? No. The key lies in the five original values listed above, which can be declared or used across languages. However, to describe this in more detail, we need to fully discuss how ECMAScript declares and implements the entire process of language.

So if we discuss "primitive value" as a type system, it will be quite confusing. This is also the reason why I raised these questions at the beginning.

Finally, I would like to emphasize one point. Function is a type. So you mentioned:

A function is not a type. A function is a function and a type is a class of an object)

It is probably the sum of all chaos. I will not talk about the first-class data types. I have talked too much about it before. Let's go over it.

Original article: http://blog.csdn.net/aimingoo/article/details/6634977

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.