判断程序输出的结果
# 判断程序输出的结果
# 下面程序的输出结果是多少?
# 题目 1
var number = 4;
var numberFactorial = (function factorial(number) {
return number === 0 ? 1 : number * factorial(number - 1);
})(number);
console.log(numberFactorial);
/**
* number 4
* number 3
* number 2
* number 1
* number 0
* return 1
* return 2
* return 6
* return 24
*
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 题目 2
function addToList(item, list) {
return list.push(item);
}
const result = addToList("nowcoder", ["hello"]);
1
2
3
4
2
3
4
# 题目 3
var array = [];
for (var i = 0; i < 3; i++) {
array.push(() => i);
}
var newArray = array.map((el) => el());
console.log(newArray);
1
2
3
4
5
6
2
3
4
5
6
# 题目 4
function a(m, n) {
var b = function (l) {
return l <= m ? l * b(l + 1) : 1;
};
return b(m - n + 1);
}
console.log(a(4, 2));
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 题目 5
const result = ["0x1", "0x2", "0x3"].map(parseInt);
console.log(result);
1
2
3
2
3
# 题目 6
const first = () => {
console.log("first");
return false;
};
const second = () => {
console.log("second");
return true;
};
console.log(first() && second());
console.log(second() || first());
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 题目 7
function getAge(...args) {
console.log(typeof args);
}
getAge(21);
1
2
3
4
5
2
3
4
5
# 题目 8
var arr = [1, 2, 3];
arr.push(arr.shift());
console.log(arr[1], arr[2]);
1
2
3
2
3
# 下面每项的返回值是什么?为什么?
null == undefined;
0.1 + 0.2 == 0.3;
typeof NaN;
typeof Function;
typeof Object;
typeof {};
"a" + 1;
"a" - 1;
Function instanceof Object;
Object instanceof Function;
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
console.log(typeof undefined == typeof NULL);
console.log(typeof function () {} == typeof class {});
1
2
2
# 执行后 a 和 b.age 的值分别为?
var a = 10;
var b = {
age: 11,
};
function fn(x, y) {
--y.age;
return --x;
}
fn(a, b);
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 正则题目
var s = "12ab3cd",
arr = s.split(/\d/);
console.log(arr[3], arr[4]);
1
2
3
2
3
# js 高级
# this 指向问题
# 题目 1
var x = 1;
var obj = {
x: 3,
fun: function () {
var x = 5;
return this.x;
},
};
var fun = obj.fun;
console.log(obj.fun(), fun());
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 题目 2
var a = 5;
function test() {
a = 0;
alert(a);
alert(this.a);
var a;
alert(a);
}
new test();
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 题目 3
function fun() {
return () => {
return () => {
return () => {
console.log(this.name);
};
};
};
}
var f = fun.call({ name: "foo" });
var t1 = f.call({ name: "bar" })()();
var t2 = f().call({ name: "baz" })();
var t3 = f()().call({ name: "qux" });
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 题目 4
- 考察箭头函数的特点
const Person = (name = "wang", age = 10) => {
this.name = name;
this.age = age;
return this.name + " is " + this.age + "years old";
};
let result = new Person("zhang", 11);
console.log(result);
1
2
3
4
5
6
7
2
3
4
5
6
7
# 题目 5
var name = "global";
var obj = {
name: "local",
foo: function () {
this.name = "foo";
}.bind(window),
};
var bar = new obj.foo();
setTimeout(function () {
console.log(window.name);
}, 0);
console.log(bar.name);
var bar3 = (bar2 = bar);
bar2.name = "foo2";
console.log(bar3.name);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16