01 js Data Types

 

 

 

1. No matter what language should be up on the data type. js no exception. So we have the basic data types, boolean, number, string, null, undefine, symbol, object, function.

2. With basic types, then how do we determine the type of a variable of Nigeria?

console.info(typeof true === 'boolean');
console.info(typeof 1 === 'number');
console.info(typeof "1" === 'string');
console.info(typeof Symbol() === 'symbol');
console.info(typeof null === 'object');
console.info(typeof undefined === 'undefined');
console.info(typeof function(){} === 'function');

3. How to judge whether it is an array, whether a class is a subclass, it can be judged by instanceof.

console.info(new Array() instanceof Array );
console.info( Array.isArray(new Array()) );

4. Finally, attach a deep copy function.

 //深拷贝函数
 function deepCopy( src ){
     var dest =  Array.isArray( src ) ? []:{};
     for(let id in src ){
         dest[id] = typeof src[id] === 'object'? deepCopy(src[id]):src[id];
     }
     return dest;
 }

 

Guess you like

Origin www.cnblogs.com/gongzhuiau/p/11462708.html