Skip to content

Commit e3c86ef

Browse files
committed
javascript questoins
1 parent 97d9cb9 commit e3c86ef

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Declaration:
2+
// function <name> ([parameters...]) {
3+
// <body>
4+
// }
5+
6+
// Expression:
7+
// var varName = function [name]([parameters...]){
8+
// <body>
9+
// };
10+
11+
// Runtime difference:
12+
// 1. Hoisting
13+
(function(){
14+
var num = randInt();
15+
16+
function randInt(){
17+
return Math.random() * Number.MAX_SAFE_INTEGER | 0;
18+
}
19+
20+
return num;
21+
}());
22+
23+
(function(){
24+
var num = randInt();
25+
26+
var randInt = function(){
27+
return Math.random() * Number.MAX_SAFE_INTEGER | 0;
28+
};
29+
30+
return num;
31+
}());
32+
33+
34+
// var num;
35+
// var randInt;
36+
//
37+
// num = randInt();
38+
// randInt = function(){ ... }
39+
40+
// 2. Stack trace
41+
function func1(cb, shouldThrow){
42+
return cb(shouldThrow);
43+
}
44+
45+
function funcDec(shouldThrow) {
46+
if (shouldThrow) {
47+
throw new Error('error @funcDec')
48+
}
49+
50+
return 'funcDec';
51+
}
52+
53+
var funcExp = function(shouldThrow){
54+
if (shouldThrow) {
55+
throw new Error('error @funxExp')
56+
}
57+
58+
return 'funxExp';
59+
};
60+
61+
setTimeout(function(){
62+
func1(funcDec, true);
63+
}, 0);
64+
65+
setTimeout(function(){
66+
func1(funcExp, true);
67+
}, 1000);
68+
69+
setTimeout(function(){
70+
func1(function (shouldThrow){
71+
if (shouldThrow) {
72+
throw new Error('error @funcAnon')
73+
}
74+
75+
return 'funcAnon';
76+
}, true);
77+
}, 2000);
78+
79+
setTimeout(function(){
80+
func1(function funcExpNamed(shouldThrow){
81+
if (shouldThrow) {
82+
throw new Error('error @funcExpNamed')
83+
}
84+
85+
return 'funcExpNamed';
86+
}, true);
87+
}, 3000);

0 commit comments

Comments
 (0)