《一》JavaScript篇
1.JavaScript有几种“数据类型”?分别是?
答案:
共7种,分别是:undefined,symbol,null,String,Boolean,Object,Number
5种基本类型:undefined,null,String,Boolean,Number
2种引用类型:Object,symbol
2.JavaScript中,null和undefined的区别?
答案:
if(undefined == null) // true
if(undefined === null) // false 可以用于区分undefined与null不等
typeof undefined // undefined
typeof null // Object
undefined是所有未赋值变量的默认值
null主动释放一个变量引用的对象,表示一个变量不再指向任何对象地址
3.JavaScript“回调函数”的本质?
答案:
回调函数的本质是将函数作为参数传递
function B(res) {
let say = '我在肯德基吃汉堡';
console.log(res);
console.log(say);
}
function A(callback) {
let say = 'B, 你在哪里?';
let res = '快来找我';
console.log(say);
callback(res);
}
A(B);
function foo(b){
let a = 2;
b(a)
}
function baz() {
let b = 3;
foo((a) => {
console.log(b,a);
})
}
baz();
A函数要使用B函数中的变量,在B函数中调用A函数,把变量传过去
function A() {
let a = 1;
B(a);
}
function B(a){
console.log(a);
}
A();
4.JavaScript中最简单的“数组去重”的方法?
Array.from(new Set([1,1,2,3,4,3])) // [1, 2, 3, 4]
5.JavaScript判断“数据类型”的几种方式?
1.typeof
可以判断出'string','number','boolean','undefined','symbol',但判断 typeof(null) 时值为 'object'; 判断数组和对象时值均为 'object'
typeof ''
"string"
typeof 2
"number"
typeof true
"boolean"
typeof undefined
"undefined"
typeof Symbol()
"symbol"
typeof []
"object"
typeof null
"object"
typeof {}
"object"
2.instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上,返回布尔值
// 定义构造函数
function C(){}
function D(){}
var o = new C();
o instanceof C; // true,因为 Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false,因为 D.prototype 不在 o 的原型链上
o instanceof Object; // true,因为 Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object // true,同上
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false,C.prototype 指向了一个空对象,这个空对象不在 o 的原型链上.
D.prototype = new C(); // 继承
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true 因为 C.prototype 现在在 o3 的原型链上
2.Object.prototype.toString.call() 常用于判断浏览器内置对象,对于所有基本的数据类型都能进行判断,即使是 null 和 undefined
Object.prototype.toString.call('')
"[object String]"
Object.prototype.toString.call(2)
"[object Number]"
Object.prototype.toString.call(null)
"[object Null]"
Object.prototype.toString.call([])
"[object Array]"
Object.prototype.toString.call({})
"[object Object]"
Object.prototype.toString.call(undefined)
"[object Undefined]"
Object.prototype.toString.call(Symbol())
"[object Symbol]"
Object.prototype.toString.call(true)
"[object Boolean]"
6.JavaScript判断是否为数组?
Array.isArray([]) // true