从零天梯:JS练习题整理

近期在学习JS基础的时候,整理了一些相对典型的JS练习题,有助于更好的理解JS相关概念。
欢迎在评论区留下你的答案~~我会不定期更新文章内容

1.传值和传址

1.1

1
2
3
4
5
6
7
8
9
10
function creat(num) {
return function () {
return num;
}
}

var arr = [creat(0),creat(1),creat(2)];
console.log(arr[0]());
console.log(arr[1]());
console.log(arr[2]());

1.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var a = 1;
var b = {
k: 30
}
var c = {
v: 30
}
function test(a,b,c) {
a = 2;
b.k = 32;
c = {
a: 20
}

}

test(a,b,c);
console.log(a);
console.log(b.k);
console.log(c.a);

2.变量提升

2.1

1
2
3
4
if(!('userName' in window)) {
var userName = 'test';
}
console.log(userName);

2.2

1
2
3
4
5
6
var a = 25;
+function() {
function a() {}
var a = 20;
console.log(a);
}();

2.3

1
2
3
4
5
6
7
function test() {}
var test = 1;
console.log(test);

var go2 = 50;
function go2() { }
console.log(go2);

2.4

1
2
3
4
5
6
7
8
9
10
11
+function() {
var a = 5;
function a() {};
console.log(a);
function b() {}
b = 6;
console.log(b);
var c = d = b;
}();
console.log(d);
console.log(c);

3.闭包、作用域、this、prototype

3.1

1
2
3
4
5
6
for(var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
},0);
}
console.log(i);

3.2

1
2
3
4
5
6
7
8
9
10
var name = "World";
(function() {
console.log('name:',name);
if(name === 'undefined') {
var name = "jack";
console.log("goodbye " + name);
}else {
console.log("hello " + name);
}
})();

3.3

1
2
3
4
5
6
7
8
9
10
11
this.a = 20;
var test = {
a: 40,
getName: function() {
return a;
}
}
var fn = test.getName;

console.log(fn());
console.log(test.getName());

3.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
this.a = 20;
var test = {
a: 40,
init: function() {
function go () {
console.log(this.a);
}
go.prototype.a = 50;
return go;
}
};

var p = test.init();
p();
new(test.init())();

3.5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
this.a = 20;
var test = {
a: 40,
init: function() {
this.a = 30;
function go () {
this.a = 60;
}
go.prototype.a = 50;
go.prototype.info = function() {
console.log(this.a);
}
return go;
}
};

var p = test.init();
p();
(new(test.init())).info();

3.6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
this.a = 20;
var test = {
a: 40,
init: function() {
function go () {
console.log(this.a);
}
go.prototype.a = 50;
return go;
}
};

var p = test.init();
p();
new(test.init())();

3.7

1
2
3
4
5
6
7
8
9
10
var name = 'globa';
function A(name) {
console.log(name);
this.name = name;
var name = '1';
}
A.prototype.name = '2';
var a = new A('3');
delete a.name;
console.log(a.name);

3.8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function fun(n,o) {
console.log(o);
return {
fun: function(m) {
return fun(m,n);
}
}
}

var a = fun(0);
a.fun(1);
a.fun(2);

var b = fun(0).fun(1).fun(2).fun(3);

var c = fun(0).fun(1);
c.fun(2).fun(3);

4.线程

4.1

1
2
3
4
5
6
7
8
9
10
11
12
13
var a = 0;
console.log(a);

setTimeout(function() {
console.log(2);
},2000);

setTimeout(function() {
console.log(1);
},0);

while (true) {
}

5.其他

5.1

1
2
3
Object.prototype.a = 20;
var m = 1;
console.log(m.a);

5.2

1
2
3
Object.prototype.a = 20;
var v = 1.0
console.log(v.a);

5.3

1
[,,].length;

5.4

1
2
3
4
5
6
7
8
function test () {
return
{
m: 1
}
}
var a = test();
console.log(a);

5.5

1
2
(function(){console.log(1);})()
(function(){console.log(2);})()

5.6

1
2
var a = 0.1 + 0.2;
console.log(a);

5.7

1
2
3
4
console.log([]+{});
console.log({}+{});
console.log([]+[]);
console.log({}+[]);

未完待续…

参考答案

1
1.1: 0, 1, 2
1.2: 1, 32, undefined
--------------------------------------
2.1: undefined
2.2: 20
2.3: 1, 50
2.4: 5, 6, 6, "c is not defined"
--------------------------------------
3.1: 3,3,3,3
3.2: undefined, hello undefined
3.3: 20, 20
3.4: 20, 50
3.5: 60
3.6: 20, 50
3.7: 3, 2
3.8: 
undefined, 0, 0
undefined, 0, 1, 2
undefined, 0, 1, 2
--------------------------------------
4.1
0,然后浏览器卡死
--------------------------------------
5.1: 各浏览器输入结果试试
5.2: 各浏览器输入结果试试
5.3: 各浏览器输入结果试试
5.4: undefined
5.5: 报错
5.6: 0.30000000000000004
5.7: 
第一个:[object Object]
第二个:[object Object][object Object]
第三个:
第四个:0

答案仅供参考。