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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more