Javascript basic types in detail

  • 2020-03-30 04:24:58
  • OfStack

There are 5 primitive values in js, 6 type-able values, and 9 native built-in constructors.

This 569 forms the basis of the js language.

The 5 original values are: number, character, Boolean, null, undefined

Typeof can judge: Numbers, characters, Boolean, object, function, undefined. Notice that null and array, tyopeof both output objects.

          Typeof cannot tell an array from an object. How do you determine the type? The use of the Object. The prototype. ToString. Apply ().

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

        The detection above gives false if the array is created in a different frame and window, and the window object is different.

          Reliable method is the if (Object. The prototype. ToString. Apply (value) = = = "[object  Array]")

      The arguments array is not an array, it is just an object with a length member property.
Arguments are not a normal array, as shown in the following example


var a = function (){
      var b = Object.prototype.toString.apply(arguments);
      console.log(b); }
a();//Output [object Arguments]


var a = function (){
      var c = [];
      var b = Object.prototype.toString.apply(c);
      console.log(b); }
a();//Output [object Array]

How does instanceof determine if it is an instance

The properties in a prototype have a constructor.

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

Although it is all one object, it is special, and the circular chain links each instance to its constructor's prototype property. There is a hidden link between the instance and the prototype property of the constructor, which is the instance's s/s. At the same time, the instance's constructor property is obtained through the constructor of the constructor.

Keep the constructor, however, so that an instanceof new has the constructor property, or you can use instanceof to determine that.


var Foo = function(){} Foo.prototype={constructor:Foo} var FooInstance = new Foo; FooInstance.__proto__=== Foo.prototype;//true FooInstance.constructor === Foo; //true

In fact, the instanceof judgment is based not on a constructor but on a prototype chain, as in the following example


 var Foo = function(){};
 Foo.prototype={};
 var FooInstance = {};
 FooInstance.__proto__=Foo.prototype;
 console.log(FooInstance instanceof Foo);//true

Use raw values, not constructors

Which values are false:false,"",null,0, minus 0,NaN,undefined, these are false and the rest are true

But notice the following example


    var a = Boolean(false);
        var b = new Boolean("");
        if (a ){console.log(a);}//Unable to output
        if (b ){console.log(b);}//Boolean {[[PrimitiveValue]]: false} new is equivalent to an object, so it's not false

The above article is a little more theoretical, but these are the basics of the javascript language, so it's important to understand.


Related articles: