This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function secretPassword() { | |
var password = 'xh38sk'; | |
return { | |
guessPassword: function(guess) { | |
if (guess === password) { | |
return true; | |
} else { | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GlobalEnvironment = { | |
EnvironmentRecord: { | |
// встроенные идентификаторы | |
Array: '<func>', | |
Object: '<func>', | |
// и т.д. | |
// пользовательские идентификаторы | |
mysteriousCalculator: '<func>', | |
toFixedTwoPlaces: '<func>', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mysteriousCalculator(a, b) { | |
var mysteriousVariable = 3; | |
return { | |
add: function() { | |
var result = a + b + mysteriousVariable; | |
return toFixedTwoPlaces(result); | |
}, | |
subtract: function() { | |
var result = a - b - mysteriousVariable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function iCantThinkOfAName(num, obj) { | |
// Это массив переменных, вместе с 2 параметрами, передаваемых | |
// "захваченными" в замкнутую функцию 'doSomething' | |
var array = [1, 2, 3]; | |
function doSomething(i) { | |
num += i; | |
array.push(num); | |
console.log('num: ' + num); | |
console.log('array: ' + array); | |
console.log('obj.value: ' + obj.value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = []; | |
for (let i = 0; i < 5; i++) { | |
result[i] = function () { | |
console.log(i); | |
}; | |
} | |
result[0](); // 0, ожидалось 0 | |
result[1](); // 1, ожидалось 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = []; | |
for (var i = 0; i < 5; i++) { | |
result[i] = (function inner(x) { | |
// дополнительный вызываемый контекст | |
return function() { | |
console.log(x); | |
} | |
})(i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
environment: { | |
EnvironmentRecord: { | |
result: [...], | |
i: 5 | |
}, | |
outer: null, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var result = []; | |
for (var i = 0; i < 5; i++) { | |
result[i] = function () { | |
console.log(i); | |
}; | |
} | |
result[0](); // 5, ожидалось 0 | |
result[1](); // 5, ожидалось 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GlobalEnvironment = { | |
EnvironmentRecord: { | |
// встроенные идентификаторы | |
Array: '<func>', | |
Object: '<func>', | |
// и т.д. | |
// пользовательские идентификаторы | |
x: 10 | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = 10; | |
function foo() { | |
var y = 20; // свободная переменная | |
function bar() { | |
var z = 15; // свободная переменная | |
return x + y + z; | |
} | |
return bar; | |
} |
NewerOlder