README
['1', '2', '3'].map(parseInt)返回的啥
答案
有了上道题的基础这个有输出什么
['10','10','10','10','10'].map(parseInt)
答案
输出什么
Promise.resolve(1)
.then(2)
.then(Promise.resolve(3))
.then(console.log)
答案
输出什么
new Promise((r, e) => {
e('123')
})
.then(
function success(res) {
throw new Error("error");
},
function fail1(e) {
console.error("fail1: ", e);
}
)
.catch(function fail2(e) {
console.error("fail2: ", e);
});
答案
1. 输出什么
async function async1() {
console.log("async1 start");
// 这里也是放入下一次微任务? 因为执行async2 拿到的是一个Promise,而CO内部需要用.then来获取resolve的值
await async2();
console.log("async1 end");
}
async function async2() {
console.log("async2");
}
async1()
console.log('start')
Promise.resolve().then(()=>console.log(123))
答案
2. 代码输出什么
const myPromise = () =>
Promise.resolve('I have resolved')
const firstFunc = () => {
myPromise().then((res) => {
console.log(res + ' first');
});
console.log('first');
}
async function secondFunc() {
// await后面的会用 Promise.resolve来包裹 然后在.then拿到值
// 相当于执行到await退出async函数的执行线程
console.log(await myPromise());
//console.log(await 1111);顺序一样
console.log('second');
}
firstFunc();
secondFunc();
解析
await xxx
// await 下面的代码
doB()
// 相当于
new Promise(resolve => {
// 创建 promise 实例的时候,放的是 await 前面的代码
// await 后面的表达式相当于 resolve(xxx)
resolve(xxx)
}).then(data => {
// await 下面的代码
doB()
})
答案
3. 一道面试题
async function async1() {
console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2');
}
console.log('script start');
setTimeout(() => {
console.log('setTimeout');
}, 0);
async1();
new Promise((resolve) => {
console.log('promise1');
resolve();
})
.then(() => {
console.log('promise2');
});
console.log('script end');
答案
4. 下面代码的输出是什么?
let a = 3;
let b = new Number(3);
let c = 3;
console.log(a == b);
console.log(a === b);
console.log(b === c);
答案
8. 输出是什么?
class Chameleon {
static colorChange(newColor) {
this.newColor = newColor;
return this.newColor;
}
constructor({ newColor = "green" } = {}) {
this.newColor = newColor;
}
}
const freddie = new Chameleon({ newColor: "purple" });
freddie.colorChange("orange");
- A:
orange
- B:
purple
- C:
green
- D:
TypeError
答案
所有对象都有原型?
答案
17. 输出是什么?
function getPersonInfo(one, two, three) {
console.log(one);
console.log(two);
console.log(three);
}
const person = "Lydia";
const age = 21;
getPersonInfo`${person} is ${age} years old`;
- A:
"Lydia"
21
["", " is ", " years old"]
- B:
["", " is ", " years old"]
"Lydia"
21
- C:
"Lydia"
["", " is ", " years old"]
21
答案
输出什么
var count = 0;
console.log(typeof count === "number");
console.log(!!typeof count === "number");
答案
输出什么
// 全局执行的
if(!("a" in window)){
var a = 10;
}
console.log(a);
答案
输出什么
// 变种题
(function(){
var x = c = b = {a:1}
})()
console.log(x.a);
console.log(c,b)
答案
输出什么
function foo(something){
this.a = something;
}
var obj1 = {
foo:foo
};
var obj2 = {};
obj1.foo(2)
console.log(obj1.a)
var bar = new obj1.foo(4);
console.log(obj1.a);
console.log(bar.a);
答案
29. 输出是什么?
const a = {};
const b = { key: "b" };
const c = { key: "c" };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
- A:
123
- B:
456
- C:
undefined
- D:
ReferenceError
答案
38. 输出是什么?
(() => {
let x, y;
try {
throw new Error();
} catch (x) {
(x = 1), (y = 2);
console.log(x);
}
console.log(x);
console.log(y);
})();
- A:
1
undefined
2
- B:
undefined
undefined
undefined
- C:
1
1
2
- D:
1
undefined
undefined
答案
输出什么?
[1, 2, 3, 4].reduce((x, y) => console.log(x, y));
答案
20. 输出是什么?
function getAge() {
"use strict";
age = 21;
console.log(age);
}
getAge();
- A:
21
- B:
undefined
- C:
ReferenceError
- D:
TypeError
答案
24. 输出是什么?
const obj = { 1: "a", 2: "b", 3: "c" };
const set = new Set([1, 2, 3, 4, 5]);
obj.hasOwnProperty("1");
obj.hasOwnProperty(1);
set.has("1");
set.has(1);
- A:
false
true
false
true
- B:
false
true
true
true
- C:
true
true
false
true
- D:
true
true
true
true