JavaScript Basic Type Detailed _ basic knowledge

Source: Internet
Author: User

JS is a total of 5 original values, 6 typeof can be judged, 9 native built-in constructors.

These 569, constitute the basis of the JS language.

5 Original values are: numbers, characters, Boolean, null,undefined

typeof can judge: number, character, Boolean, object,function,undefined. Note NULL and array, tyopeof output object.

typeof can not distinguish between the group and the object, how to judge the type? Use Object.prototype.toString.apply ().

if (value&&typeof value = = = ' object ' &&value.constructor = = Array)

The above detection if the array created in different frames and windows will give the False,window object the same.

A reliable method is if (Object.prototype.toString.apply (value) = = "[Object Array]")

The arguments array is not an array, he is just an object with a length member property.
As shown in the following example arguments is not a normal array

Copy Code code as follows:

var a = function () {
var B = Object.prototype.toString.apply (arguments);
Console.log (b);

}
A ();//Output [object Arguments]

Copy Code code as follows:

var a = function () {
var c = [];
var B = Object.prototype.toString.apply (c);
Console.log (b);

}
A ();//Output [object Array]

Instanceof is how to determine whether an instance

The attributes inside the prototype are constructor.

The default prototype property is an object that can be set to any complex value and ignored to the original value.

Though he was full of one object, but he was special, the circular chain links each instance to the prototype property of its constructor. There is a hidden link between the instance and the prototype property of the constructor, which is the __proto__ of the instance. The constructor property of the instance is obtained by the constructor of the constructor prototype.

However, to preserve constructor, this allows the instance of new to have constructor properties, or it can be judged using instanceof.

Copy Code code as follows:

var Foo = function () {}

Foo.prototype={constructor:foo}

var fooinstance = new Foo;

fooinstance.__proto__=== Foo.prototype;//true

Fooinstance.constructor = = Foo; True

Actually instanceof judgment is not based on constructor, but on the basis of the prototype chain, as in the following example

Copy Code code as follows:

var Foo = function () {};
foo.prototype={};
var fooinstance = {};
Fooinstance.__proto__=foo.prototype;
Console.log (fooinstance instanceof Foo);//true

Use original values, do not use constructors

Which values are False:false, "", null,0,-0,nan,undefined, these are false, others are true.

But please note the following example

Copy Code code as follows:

var a = Boolean (false);
var B = new Boolean ("");
if (a) {Console.log (a);} Cannot output
if (b) {console.log (b);} Boolean {[[Primitivevalue]]: false} New one is equivalent to an object, not false

The above article is a little more theoretical, but these are the basics of JavaScript language, so be sure to understand.

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.