JavaScript经典前端面试题---数据类型

134 阅读4分钟

一: Js的数据类型

6中基本类型:NumberStringBooleanNullUndefinedSymbol

1中引用类型: Object,里面包含Function Array Date

二: 如何判断数据类型

1. typeof关键字

typeof 123   //Number
typeof 'hello' //String
typeof false  //Boolean
typeof undefined //Undefined
typeof null  //Object
typeof {}  //Object
typeof [] //Object
typeof function(){console.log('a')} //Function
typeof new Date() //Object
typeof new RegExp() //Object

总结:

  • 对于基本类型,除了null,均可返回正确的结果
  • 对于引用类型,只有function能返回正确的结果

2. instanceof 是用来判断 A 是否为 B 的实例

[] instanceof Array; // true
{} instanceof Object;// true
new Date() instanceof Date;// true
[] instanceof Object //true

Function Person() {}
new Person() instanceof Person // true

[]既是Array的实例,又是Object的实例,解释原因如下:

从 instanceof 能够判断出 [ ].proto  指向 Array.prototype,而 Array.prototype.proto 又指向了Object.prototype,最终 Object.prototype.proto 指向了null,标志着原型链的结束。因此,[]、Array、Object 就在内部形成了一条原型链:

image.png

总结:instanceof只能判断两个对象是否属于实例关系 , 而不能判断一个对象实例具体属于哪种类

3.constructor

当函数F被定义时,JS引擎会给F添加prototype属性,然后在prototype上再添加一个constructor属性,并指向F的引用

''.constructor == String  //true
new Number(1).contructor == Number //true
  • null 和 undefined 是无效的对象,因此是不会有 constructor 存在的,这两种类型的数据需要通过其他方式来判断。
  • 函数的 constructor 是不稳定的,这个主要体现在自定义对象上,当开发者重写 prototype 后,原有的 constructor 引用会丢失,constructor 会默认为 Object
function Person() {}
var p = new Person
p.constructor == Person  //true
Person.prototype = {a: 'hello'}
p.constuctort == Person //false

4.toString()

toString() 是 Object 的原型方法,调用该方法,默认返回当前对象的 [[Class]] 。这是一个内部属性,其格式为 [object Xxx] ,其中 Xxx 就是对象的类型。

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(Symbol()); //[object Symbol]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]

三: Symobl介绍

Symbol是ES6中新加入的数据类型,表示独一无二的值,可以用来防止属性名称冲突。

特性1:Symbol值通过Symbol函数生成,使用typeof,结果为"symbol"

var a = Symbol();
console.log(typeof a) //symbol

特性2:Symbol 函数前不能使用 new 命令,否则会报错。这是因为生成的 Symbol 是一个原始类型的值,不是对象

特性3:instanceof 的结果为 false

var a = Symbol(); 
console.log(a instanceof Symbol) //false

特性4:Symbol 函数可以接受一个字符串作为参数,表示对 Symbol 实例的描述

var a = Symbol('des'); 
console.log(a) //Symbol(des)

特性5:如果 Symbol 的参数是一个对象,就会调用该对象的 toString 方法,将其转为字符串,然后才生成一个 Symbol 值

const obj = {toString(){return 'abc'}} 
const a = Symbol(obj)
console.log(a) //Symbol(abc)

特性6:Symbol 值可以作为标识符,用于对象的属性名,可以保证不会出现同名的属性。

var mySymbol = Symbol();
//第一种写法:
var a = {};
a[mySymbol] = 'hello';
//第二种写法:
var a = {
    [mySymbol]: 'hello'
};
//第三种写法:
var a = {};
Object.defineProperty(a,mySymbol,{value: 'hello})

console.log(a[mySymbol]) //hello

特性11:使用同一个 Symbol 值,可以使用 Symbol.for。它接受一个字符串作为参数,然后搜索有没有以该参数作为名称的 Symbol 值。如果有,就返回这个 Symbol 值,否则就新建并返回一个以该字符串为名称的 Symbol 值。

var s1 = Symbol.for('saucxs');
var s2 = Symbol.for('saucxs');
console.log(s1 === s2); // true

四: null和undefined的区别

1.null表示没有队形,该处不应该有值

  • 作为函数的参数,表示函数的参数不是对象
  • 作为原型链的终点

2.undefined表示缺少值,此处应该有值,但是还没有定义

  • 变量被声明了,但是没有赋值
  • 调用函数时,该提供参数没有提供参数
  • 对象没有赋值的属性
  • 函数没有返回值时,默认返回undefined

3.null和undefined转换成Number数据类型

  • null默认转换成0
  • undefined默认转换成NaN

五: 数据类型转换

1.相等 == 和 全等 ===

  • ==判断值是否相等

  • ===判断值和类型是否完全相等 2.显式类型转换和隐式类型转换

  • 显式类型转换: Number(), parseInt() ,parseFloat() ,String() ,Boolean()

  • 隐式类型转换:通过运算符的计算得到

3.包装类型

  • 普通变量不能直接调用属性和方法
  • 对象可以直接调用属性和方法
如:
var string = 'hello'; //基本类型
var num = new String("hello") //基本包装类型