前端笔试题
# 前端笔试题
var str1 = "hello world";
var str2 = String("hello world");
var str3 = new String("hello world");
console.log(str1 === str2); // true
console.log(str1 === str3); // false
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# parseInt
['10', '10', '10', '10', '10'].map(parseInt)
的输出值是什么?[10, NaN, 2, 3, 4]
['1','2','3'].map(parseInt)
[1, NaN, NaN]
# 运算
let a = (b = 10);
(function() {
let a = (b = 20);
})();
console.log(a); // 10
console.log(b); // 20
// 连等操作是从右向左执行的
1
2
3
4
5
6
7
2
3
4
5
6
7
# 闭包
var name = 1;
(function() {
console.log(name + this.name); // browser: -> 'undefined1'; Node.js: -> NaN
var name = "2";
console.log(name + this.name); // browser: -> '21'; Node.js: -> '2undefined'
})();
1
2
3
4
5
6
2
3
4
5
6
var b = 10;
(function b() {
b = 20;
console.log(b); // 输出:[Function: b]
})();
console.log(b); // 输出:10
1
2
3
4
5
6
2
3
4
5
6
for (var i = 0; i < 5; i++) {
setTimeout(() => {
console.log(i);
}, i);
} // 输出:5,5,5,5,5
for (let i = 0; i < 5; i++) {
setTimeout(() => {
console.log(i);
}, i);
} // 输出:0,1,2,3,4
for (var i = 0; i < 5; i++) {
setTimeout(
(i) => {
console.log(i);
},
i,
i
);
} // 输出 0,1,2,3,4
for (var i = 0; i < 5; i++) {
(function(i) {
setTimeout(
(i) => {
console.log(i);
},
i,
i
);
})(i);
} // 自执行函数,输出 0,1,2,3,4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# this
const person1 = {
name: "person1",
say: function() {
console.log(this.name);
},
eat: () => {
console.log(this.name);
},
};
const person2 = {
name: "person2",
};
person1.say(); // person1
person1.say.call(person2); // person2
person1.eat.call(person2); // undefined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# typeof
// 这两种方式的区别?typeof 判断
const str1 = "abc";
const str2 = new String("abc");
1
2
3
2
3
更新时间: 2/10/2022, 7:21:32 PM