Last active
March 30, 2019 09:57
-
-
Save beardlessman/652a12b267ceacee8320fb7f09e20bec to your computer and use it in GitHub Desktop.
Собеседование Леши: замыкания, промисы, прототипы
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
// Отметить идентификаторы, доступные через замыкания | |
// Ответ: weapon, name | |
function Samurai(name) { | |
var weapon = 'katana'; | |
this.getWeapon = function() { | |
return weapon; | |
}; | |
this.getName = function() { | |
return name; | |
}; | |
this.message = `${name} wielding a ${weapon}`; | |
this.getMessage = function() { | |
return this.message; | |
}; | |
} | |
var samurai = new Samurai('Hattori'); | |
samurai.getWeapon(); | |
samurai.getName(); | |
samurai.getMessage(); | |
// Что будет выведено в консоль? | |
// Ответ: undefined, 5 | |
function test() { | |
console.log(window); | |
var window = 5; | |
console.log(window); | |
} | |
test(); | |
// Что выведется в консоли? | |
// Ответ: 'Fulfilled: result' | |
let promise = new Promise((resolve, reject) => { | |
setTimeout(() => resolve('result'), 1000); | |
setTimeout(() => reject(new Error('error')), 2000); | |
}); | |
promise.then( | |
result => console.log('Fulfilled: ' + result), | |
error => console.log('Rejected: ' + error), | |
); | |
// Реализовать параллельную и последовательную загрузку ресурсов | |
let urls = ['1.json', '2.json']; | |
// Ответ: | |
// параллельно | |
Promise.all(urls.map(item => fetch(item))).then(console.log); | |
// последовательно | |
let chain = Promise.resolve(); | |
let results = []; | |
urls.forEach(function(url) { | |
chain = chain | |
.then(() => fetch(url)) | |
.then(result => { | |
results.push(result); | |
}); | |
}); | |
chain.then(() => { | |
console.log(results); | |
}); | |
// Как бы выглядел аналогичный код на прототипах? | |
class Foo { | |
constructor(title) { | |
this.title = title; | |
console.log("kek"); | |
} | |
} | |
class Bar extends Foo {} | |
const foo = new Foo('foo'); | |
const bar = new Bar('bar'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment