preface

I am male, love women, not computer specialized graduation, the university entrance exam errors in the bottom 2 and 3 pick on the embarrassment of fraction, parents feel that a good three unpopular major may be more suitable for me, so choose a possible 99% have never heard of profession: the lightning protection science and technology, namely research how lightning protection, prevention of ray’s day! This major has only two school, I thought after four years with his to meteorological undertakings in the country, we learned that the late due to policy changes, so the weather bureau internal important adjustment, market liberalisation of lightning protection, home and no meteorological contacts I felt himself into a lightning protection company also can, but the basic lightning protection company is a small workshops, ah, I’m so damn competitive! By a strange combination of circumstances, relatives at home let me see the front end of things, self-study, from then on to open a new chapter of my life! Since I was dismissed from my job some time before the outbreak, I felt that my foundation was still weak in the interview, so I was prepared to consolidate it, so I have the idea of relearning JS foundation.

My Github: github.com/TomatoesMan… I hope I can give a star a boost. Thank you

My blog: https://TomatoesMan.github.io

In the next few days, we’ll go over the basics of JS and start with the first part, so we’ll start with the basics of JS, basic types and reference types

1, what is the base type (value type, primitive type)

The type stored in the stack is the base type, because the base type storage space is small, stored in the stack is easy to find, and is not easy to change

What are the base types (value type, primitive type) in js

Undefined, Null, Boolean, Number, String, Symbol (new type in ES6) // Undefined and Null are subtypes of all typesCopy the code

2. What are reference types

An object consisting of multiple values, the reference data type is stored in the heap, that is, the value at the stored variable is a pointer to the memory address of the stored object. The reason it exists in the heap is that the size of the reference value changes, so it cannot be put on the stack, otherwise it will slow down the query of the variable

What are the reference types in js

Object;Copy the code

3, How to determine the type

3.1 typeof

Specific use:

console.log(typeof a); //'undefined'
console.log(typeof true); //'boolean'
console.log(typeof "123"); //'string'
console.log(typeof 123); //'number'
console.log(typeof NaN); //'number'
console.log(typeof null); //'object'
let obj = new String(a);console.log(typeof obj); //'object'
let fn = function () {};
console.log(typeof fn); //'function'
console.log(typeof class c {}); //'function'
let sym = Symbol(a);console.log(typeof sym); //'symbol'Copy the code

The implementation principle is as follows:

In JavaScript, if the first three bits of binary are all zeros, it will be considered as an object. The binary representation of null is all zeros, and naturally the first three bits are also zeros, so typeof will return "object".Copy the code

So the return value of Typeof null is object, which is a “legacy problem.” Because typeof is not very accurate in determining the typeof reference, we use other methods.

3.2 instanceof

In general, instanceof is used to determine whether an instance belongs to a particular type.

console.log(1 instanceof Number); //false
console.log("1" instanceof String); //false
console.log(true instanceof Boolean); //false
console.log(function Foo() {} instanceof Function); //true
console.log({} instanceof Object); //true
console.log([] instanceof Array); //trueCopy the code

As can be seen from the above results, instanceof is very accurate for reference types, but not for basic types. The reason is that the instanceof operator tests whether an object has a constructor’s prototype property in its prototype chain. 1, ‘1’, and true are not instances, so they are false. As follows:

console.log(new Number(1) instanceof Number); //true
console.log(new String("1") instanceof String); //true
console.log(new Boolean(true) instanceof Boolean); //trueCopy the code

But for undefined and null, it’s special

console.log(new null(a)instanceof Null); //Uncaught TypeError: null is not a constructor
console.log(new undefined(a)instanceof Undefined); //Uncaught TypeError: undefined is not a constructorCopy the code

The reason is that undefined and null are not constructors

3.3 the constructor

Specific use:

console.log((1).constructor === Number); //true
console.log("1".constructor === String); //true
console.log(true.constructor === Boolean); //true
console.log([].constructor === Array); //true
console.log({}.constructor === Object); //true
console.log(function Foo() {}.constructor === Function); //trueCopy the code

It seems to be accurate, but there is a drawback: when creating an object that changes its prototype, it is no longer accurate!

function Foo() {}
Foo.prototype = new Array();
let foo = new Foo();
console.log(foo.constructor === Array); //trueCopy the code

This is because each function in js has a prototype property that points to its prototype object. Prototype. constructor refers to the constructor of the object, and when the prototype of the object is changed, the constructor of the function also changes

3.4 the Object. The prototype. ToString. Call ()

Specific use:

console.log(Object.prototype.toString.call(1)); //[object Number]
console.log(Object.prototype.toString.call("1")); //[object String]
console.log(Object.prototype.toString.call(true)); //[object Boolean]
console.log(Object.prototype.toString.call([])); //[object Array]
console.log(Object.prototype.toString.call({})); //[object Object]
console.log(Object.prototype.toString.call(function Foo() {})); //[object Function]
console.log(Object.prototype.toString.call(undefined)); //[object Undefined]
console.log(Object.prototype.toString.call(null)); //[object Null]
console.log(Object.prototype.toString.call(Symbol())); //[object Symbol]Copy the code

This is the most accurate way to determine the type, even if you change the prototype. Here’s the story behind this method: There is a toString method on each Object stereotype. When called, this method performs three steps: 1. Get the class name of the Object (Object type); 2

Why use the call method?

Because Array, a String type to its inherited rewrites the toSring method on the prototype, we want to be the result of no return, so we call way, will Object. The prototype. The Object on the toString pointing in the direction of change, In order to accurately judge the type.

Object. The toString and Object. The prototype. What is the difference between the toString?

Object.toString is a method of the Object constructor that returns a function

Object.toString({}); //"function Object() { [native code] }"Copy the code

Object. The prototype. The toString method on the Object prototype, returns the type string, the result of is what we want

Object.prototype.toString({}); //"[object Object]"Copy the code

If you find it helpful, I hope you can give it to my blog on Github:
Github.com/TomatoesMan…A star, thank you!