JS基础知识点及常考面试题(1)
- 原始类型有哪几种?null是对象嘛?
- 对象类型和原始类型的不同之处?函数参数是对象会发生什么问题?
- typeof是否能正确判断类型?instanceof 能正确判断对象的原理是什么?
- 类型转换有哪些?
- 如何正确判断this? 箭头函数的this 是什么?
1.原始类型有哪几种?null是对象嘛?
原始类型有boolean, null, undefined, string, number, symbol
null不算是对象,虽然type of null为object, 这是JS存在的一个悠久的BUG.
注: 原始类型存储的存储的都是值,是没有函数可以调用的,
undefined.toString()会报错, 但是 '1'.toString()可以使用,是因为'1'已经被强制转换成String类型(对象类型)
2.对象类型和原始类型的不同之处?函数参数是对象会发生什么问题?
原始类型存储的是值,对象类型存储的是地址(指针).
函数参数是对象的时候,是传递的对象指针的副本。
function test(person) {
person.age = 26
person = {
name: 'yyy',
age: 30
}
return person
}
const p1 = {
name: 'yck',
age: 25
}
const p2 = test(p1)
// p1 作为副本传入test函数,但副本改变,与p1无关
console.log(p1) // -> { name: 'yck', age: 25 }
console.log(p2) // -> person = { name: 'yyy', age: 30 }
3.typeof 是否能正确判断类型?instanceof 能正确判断对象的原理是什么?
typeof 对于原始类型来说,除了null 都可以显示正确都类型。
typeof 1 // 'number'
typeof '1' // 'string'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol() // 'symbol'
typeof 对对象来说, 除了函数都会显示Object, 所以不能正确都判断类型
typeof [] // 'object'
typeof {} // 'object'
typeof console.log // 'function'
instanceof 能正确判断对象都原理是通过原型链来判断,但直接判断原始类型是不行。
注:
如果需要instanceof 可以支持判断原始类型,使用Symbol内置属性hasInstance
class PrimitiveString {
static [Symbol.hasInstance](x) {
return typeof x === 'string'
}
}
console.log('hello world' instanceof PrimitiveString) // true
还可以用toString() + call方法来判断
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]
Object.prototype.toString.call(new Error()) ; // [object Error]
Object.prototype.toString.call(document) ; // [object HTMLDocument]
Object.prototype.toString.call(window) ; //[object global] window 是全局对象 global 的引用
function typeJudge(data) {
// 先通过toString()判断,
return Object.prototype.toString.call(data).replace(/^\[object/, '').replace(/\]$/, '').toLocaleLowerCase()
}
4.类型转换有哪些?
转boolean
在条件判断时, 除了undefined, null, false, NaN, "", 0, -0, 其他所有的值和所有对象转为true.
转string [] 或者 [''] => '', 关键字(new, function) => 抛错, 对象 => '[object Object]'.
除了数组,关键字,对象以外,都直接外层加引号就可以了
String([]) // ''
String(['']) // ''
String(null) // 'null'
String(NaN) // 'NaN'
转number
[] 或者 [''] => 0, 关键字(new, function) => 抛错, null => 0, undefined => NaN
对象转原始类型 优先级 Symbol.toPrimitive > valueOf() > toString()
四则运算符
- 运算中其中一方为字符串,那么就会把另一方也转换为字符串
- 如果一方不是字符串或者数字,那么会将它转换为数字或者字符串
5.如何正确判断this? 箭头函数的this 是什么?
- 对于直接调用 foo 来说,不管 foo 函数被放在了什么地方,this 一定是 window
- 对于 obj.foo() 来说,我们只需要记住,谁调用了函数
- 对于 new 的方式来说,this 被永远绑定在了 c 上面,不会被任何方式改变 this
- 箭头函数中的 this 只取决包裹箭头函数的第一个普通函数的 this。
注:new > bind这些函数 > obj.foo() > foo() 直接调用
bind函数的this取决于第一次调用