JS data types that come up with code optimization

Source: Internet
Author: User

Introduction:

Last WednesdayCodeThere is a common code, such:

If (test = "") {dothis ();} else {dothat ()} -----> can be simplified! Test? Dothis (): dothat ();

If (test = "") {dothis ()} -----> can be simplified! Test & dothis ();

The root cause of this problem is to determine whether the value of test is false or true. If test = "", execute the dothis () function, this is actually a type judgment problem. It seems that "" is a false, so I think, is it something else?

Yes, for example, 0, false, null, undefined, Nan. I wrote an example to test it:

<! Doctype HTML> If(False|Null| Undefined | ''| 0 |NAN) {alert (1);}Else{Alert (2);}</SCRIPT>  

The above code is output as result 2 in various browsers. It can be seen that JS treats these 6 types of values as false, and there is nothing else. Currently, I only want to think about this, however, there are many variants and many applications. In functions, statements, and so on ..

JS Data Type

For this topic, let's take a look at several questions first. If you have a clear understanding of these questions, skip this step without further reading. If not, we recommend that you take a look.

1) if the judgment function? What is the connection between a function and an object?

2) What is the difference between typeof and instanceof?

3) What is the difference between undefined and null?

4) which basic data types does JS have?

5)What are undefined, undefined, and 'undefined' respectively?

6) What is the value of typeof null and null instanceof object?

There are many different statements about types in different books,

First let's take a look at what ECMA said, The ECMA262-5 said ecmascript type is undefined, null, Boolean, String, number, object, each type corresponds to a value, respectively undefined, null, true/false, "elements", number,

A collection of prototies. Each type has a corresponding value, which are 'undefined', 'null', 'boolean', 'string', 'number', and 'object '. this statement does not explain the type of a function, the difference between null and undefined.

In the first version of the Javascript advanced tutorial (P11), JavaScript only has the original basic data types (undefined, null, Boolean, String, number) in step 5, which is obviously incorrect. So what is an object? In the second version, the object is added, that is, the object is
A complex data type. All values will be one of the above six types. I think this statement is more reliable, but what if it is a function? This makes it unclear which category it belongs.

Aimingoo refers to two types of systems in Javascript. One is the basic data type, and the other is the object type system. The basic data types include six types: undefined, String, null, Boolean, function, object, the basic data type is through typeof

The object type system is based on the basic type system, and the object type system detects through instanceof. The object type corresponds to the basic data type, for example, string-> sting, Boolean-> Boolean, number-> number, they only map, essentially

Is different. undefined, String, null, and Boolean are value types. Function, object is of the reference type. The string, Boolean, and number classes are all object types and are subclasses of objects. Function () is also a reference type. All reference types are subclasses of objects.

I think it is more reliable to understand the data types in JS through two types of systems. First, it uses typeof and instanceof to distinguish between basic data types and object types. Different object types Use A instanceof type to determine whether they are their corresponding types,

For example, alert (typeof null) returns "object", but alert (null instanceof object) returns false. This indicates that null is an object, but it is not a subclass of the object. Secondly, it can answer "aaaa ". why is length 4? This is a packing operation,

When the. Operator is foundWhen the left operand is a "Value Type" data, it is implicitly called the object (x) process to convert it into an object, so "aaaa ". length is implicitly converted to string ("aaaa "). length, while string () has the Length attribute. Therefore, this value is 4.

Back to the several questions at the beginning of this article, we can clearly explain:

1) if the judgment function? What is the connection between a function and an object?

By using typeof, both function and object are reference types. The basic data type of function, object is object type, and function is a subclass of object.

2) What is the difference between typeof and instanceof?

Typeof is used to determine the basic data type, and instanceof is used to determine the object type. However, function is a bit special. Alert (typeof function () {}) // "function" alert (function () {} instanceof object) // true

3) What is the difference between undefined and null?

One is the value type and the other is the reference type.

4) which basic data types does JS have?

6 types: undefined, Boolean, String, number, object, Function

5) What are undefined, undefined, and 'undefined?

Undefined is a type, undefined is a value, and 'undefined' is a type name.

6) What is the value of typeof null and null instanceof object?

'Object', false

You are welcome to make a brick.

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.