一、JavaScript Map 和 Object 的区别(https://www.cnblogs.com/ysx215/p/11387938.html)
1、Key filed
在 Object 中, key 必须是简单数据类型(整数,字符串或者是 symbol),而在 Map 中则可以是 JavaScript 支持的所有数据类型,也就是说可以用一个 Object 来当做一个Map元素的 key。
2、元素顺序
Map 元素的顺序遵循插入的顺序,而 Object 的则没有这一特性。
3、继承
Map 继承自 Object 对象。
二、# 手写事件总线 EventBus
三、面试题
第一题:
图片1.png结果为:1,3,2
第二题:
图片2.png调用fun(2,3,4)
结果为:[[Array(4),Array(4), Array(4)],[Array(4),Array(4),Array(4)]]
第三题:
function Foo() {
getName = function () { alert (1); };
return this;
}
Foo.getName = function () { alert (2);};
Foo.prototype.getName = function () { alert (3);};
var getName = function () { alert (4);};
function getName() { alert (5);}
Foo.getName(); // 2
getName(); // 4
Foo().getName(); // 1
getName(); // 1
new Foo.getName();// 2
new Foo().getName(); // 3
new new Foo().getName();// 3
var a=0,b=0;
function A(a){
A = function(b){
alert(a+b++);
}
alert(a++);
}
A(1); // 1
A(2);// 4
第四题
// [{a:1, b:1, c:4}, {a:2, b:1, c:4}, {a:3, b:1, c:4}]
var obj = {a:[1,2,3], b:1, c:4};
function test(obj, key) {
const {[key]: arr, ...rest} = obj;
const result = [];
for(let i in arr) {
result.push({[key]: arr[i], ...rest});
}
console.log(result);
return result;
}
test(obj, 'a');
程序题
var a = {};
var b = {key: "123"};
var c = {key: "456"};
a[b] = "b";
a[c] = "c";
console.log(a[b]); // 'b'
console.log(a);
console.log(Object.keys(a));// ["b"]
var a = {};
var b = Symbol('123');
var c = Symbol('123');
a[b] = "b";
a[c] = "c";
console.log(a[b]);
console.log(Object.keys(a));
var num = 4;
function fn() {
console.log(num);
console.log(this.num);
var num = num - 1;
console.log(num);
this.num = this.num--;
console.log(this.num);
}
fn();// undefined 4 3 3
function fn() {};
let o = new fn();
o.__proto__ === fn.prototype ;
o.__proto__.__proto__ === Object.prototype;
fn.__proto__ === Function.prototype;
fn.__proto__.__proto__ === Object.prototype;
Function.__proto__ === Function.prototype;
Object.__proto__ === Function.prototype;
function myNew() {
let obj = {};
obj.__proto__ = myNew.prototype;
let result = myNew.apply(obj,args);
return typeof result === object ? result : obj;
}
/* 2. */
var num = 4;
function fn() {
console.log(num);
console.log(this.num);
var num = num - 1;
console.log(num);
this.num = this.num--;
console.log(this.num);
}
fn();
/* 非严格模式 */
undefined 4 3 3
/* 严格模式 */
第一次 console.log(num) 报错
/* 5. */
async function async1() {
console.log('1');
await async2();
console.log('2');
}
async function async2() {
console.log('3');
}
console.log('4');
setTimeout(function() {
console.log('5');
Promise.resolve().then(function() {
console.log('6');
});
}, 0);
setTimeout(function() {
console.log('7');
Promise.resolve().then(function() {
console.log('8');
});
}, 0);
async1();
new Promise(function(resolve) {
console.log('9');
resolve();
}).then(function() {
console.log('10');
});
console.log('11');
// 4 1 3 9 11 2 10 5 6 7 8
网友评论