-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_28.js
More file actions
37 lines (28 loc) · 1.12 KB
/
item_28.js
File metadata and controls
37 lines (28 loc) · 1.12 KB
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
34
35
36
37
// Avoid relying on 'toString' method of functions
//
// JavaScript functions can reproduce their source code as a string!!!
// Mind === Blown; // true
var myF =(function(x) {
return x + 1;
}).toString(); // "function(x) {\n return x + 1;\n}"
console.log(myF);
// but this has limitations
// especially with functions produced by built-in libraries of the host
// environment
var hiddenF = (function(x) {
return x + 1;
}).bind(16).toString(); // "function () {\n [native code]\n}"
console.log(hiddenF);
// in many host envs, the 'bind' function is implemented in another programming
// language (typically C++), it produces a compiled function that has no
// Js code to reveal.
// toString does not provide a representation of closures that preserve the
// values associated with their inner variable references
var closureF = (function(x) {
return function(y) {
return x + y;
}
})(42).toString();
console.log(closureF); //function(y) {\n return x + y;\n}
// notice how the resultant string still contains a variable reference to x,
// even though the function is actually a closure that binds x to 42