"JavaScript supplements VI" Drill down into JavaScript data types and object types

Source: Internet
Author: User

Through the previous chapters, we have figured out several important features of JavaScript, the function of class concept, the "function" instantiation of the object, the prototype chain, the definition of function, and so on. JavaScript as a high-level scripting language is also an object-oriented language, but the data types in JavaScript are not like the "Everything is Object" in Java, and we'll delve into some of the data types and their judgments in JavaScript. Let ' s start learning, move it!

Drill down into JavaScript data types 1, 5 basic data types, and 1 complex data types

5 basic data types include: Undefined, Null, Boolean, number, and string. 1 Complex data types: Object with 8 Standard object types: Function, number, Math, String, Date, Array, Boolean, and REGEXP. Javascript supports custom object types. is not very rich, haha, enough to engage in a lifetime. Javascript is a weakly typed language, which is a type that can be implicitly converted in code.

2. Judging data Type 2.1 typeof

Judging the type of object The first thing we think of is TypeOf, the same typeof is a relatively reliable comparison method.

Usage: typeof expression

We generally use this kind of judgment: typeof x = = = ' undefined '?...

1 var a = ' + 123; 2 console.log (typeof//true
2.2 Strong type judgment

The three equals "= = =" can be used to determine whether the type and value of two expressions are equal. Equivalent to A = = B && typeof a = = = typeof b

1 var a = ' + 123; 2 var b = 123; 3 // false
2.3 Constructor Judgment

Similarly we can use the constructor property to determine the type.

1 var b = [1, 2, 3]; 2 // true
2.4 Instanceof Judgment
1 var b = [1, 2, 3]; 2 instanceof Array); // true

Note : 2.3 2.4 is limited to judging complex data (object) types and cannot be used for basic data types.

1 var a = 123; 2 var New Number (123); 3 instanceof // false 4 instanceof // true
3, initialize the object method 3.1 New keyword

The function uses the new keyword to add either an object type or an object of one type.

1 varA =NewObject ();2 varb =NewString (' 123 ');3 varc = String (' 123 ');4Console.log (typeofa);//Object5Console.log (typeofb);//Object6Console.log (typeofc);//string

In the code example, a B is an object type, similar to object in Java, and only a normal string type is defined by the constructor string (). This can also explain the difference between the new function () and the function ().

3.2 Object Literals

Object literal defines an object that is actually defined as a JSON-formatted object. It does not need to be instantiated, it is stored directly in the object's data format after the code has been parsed, so it is more efficient to store.

1 var a = {2     0:1,3     1:0,4     a:10,  5     function() {6        alert (A.A); 7     }8}9 console.log (typeof a); // Object
4. Summary

Through the study of this article and the previous chapters of the research, I believe that you look at JavaScript is not a few simple functions of the language. Through the prototype chain prototype interface We can extend the methods of standard objects, such as Array.prototype.in_array. We can directly define the Array ("Jack", "Marry", "Rose") through the constructor of the standard object, or it can be extended directly to the generic "object", such as the new Array ("Jack", "Marry", "Rose"). Well, JavaScript supplements articles for a while, in fact, the depth of these articles can only be used to deal with the interview, JavaScript, a sophisticated scripting language and a lot of content worth thinking about. But as long as people through these articles can learn something, "the author" is satisfied. Finally, I hope that Members will make more valuable comments. Next I'm going to start learning about Linux and C + +, and I'll keep track of some of my learning notes and new project content, and look forward to it.

"JavaScript supplements VI" Drill down into JavaScript data types and object types

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.