Published
Edited
Nov 22, 2021
1 star
Insert cell
Insert cell
Insert cell
Insert cell
{
const number = 179;

function makeCounter() {
//[[Scope]].number = 179
const step = 57
return function() {
//[[Scope]].step = 57
//[[Scope]].[[Scope]].number = 179
return number + step;
};
}
let counter = makeCounter();
console.log(counter());
// makeCounter.[[Scope]] = window
// window.[[Scope]] = null
}

Insert cell
Insert cell
Insert cell
{
let phrase = 'Привет';
function sayHi(name) {
console.log(phrase + ', ' + name);
}
sayHi('Вася'); // Привет, Вася
phrase = 'Пока';
sayHi('Вася'); // Пока, Вася
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const funcs = []
for (let i = 0; i < 3; i++) {
funcs.push(() => console.log(i));
// У каждой итерации цикла своё собственное лексическое окружение
}
funcs.forEach(f => f()) // выведет 0 1 2
}
Insert cell
Insert cell
{
// сделать какую-нибудь работу с локальными переменными, которые не должны быть видны снаружи
let message = "Hello";
console.log(message); // Hello
}
//console.log(message); - ERROR!!!
Insert cell
Insert cell
{
let counter = 0;
const func = () => {
console.log(counter++);
if (counter < 10) {
//console.log(localVar); - ReferenceError: Cannot access 'localVar' before initialization
const localVar = 179;
func();
}
};
func();
}
Insert cell
Insert cell
{
function badFibonacci(n) {
return n <= 1 ? n : badFibonacci(n - 1) + badFibonacci(n - 2);
}

return badFibonacci(10);
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more