10. Что не так?
Constructor.prototype.method = function () {
setTimeout(function () {
this.anotherMethod();
}, 5000);
};
Constructor.prototype.anotherMethod = function () { };
01.
02.
03.
04.
05.
06.
10
11. this === хозяин метода setTimeout
Constructor.prototype.method = function () {
setTimeout(function () {
this.anotherMethod();
}, 5000);
};
Constructor.prototype.anotherMethod = function () { };
Uncaught TypeError: undefined is not a function
01.
02.
03.
04.
05.
06.
11
21. Что не так?
var some = 'cool and not ';
function foo() {
console.log(some);
var some = 'awful!';
console.log(some);
}
foo();
Output: cool and not awful!
01.
02.
03.
04.
05.
06.
07.
21
22. Hoisting
var some = 'cool and not ';
function foo() {
console.log(some);// some === undefined
varsome = 'awful!';
console.log(some);
}
foo();
Output: undefined awful!
01.
02.
03.
04.
05.
06.
07.
22
23. Что плохого?
for (var index = 0; index < 1000000; index++) {
console.log(index);
}
01.
02.
03.
23
24. Длинный цикл
for (var index = 0; index < 1000000; index++) {
console.log(index);
}
Страница не обрабатывает события
Сервер не принимает новые подключения
01.
02.
03.
24
25. Длинный цикл.Решение
var index = 0;
function next() {
if (index >= 1000000) { return; }
console.log(index++);
setTimeout(next, 0);
}
next();
01.
02.
03.
04.
05.
06.
07.
25
26. Что нет так?
var arr = [];
arr[5] = 'some';
console.log(arr.length);
Output: 1
01.
02.
03.
26
31. Что не так?
function foo () {
return
{
name: 'Batman'
};
}
console.log(foo());
01.
02.
03.
04.
05.
06.
07.
31
32. Конечно не Batman,он великолепен!
function foo () {
return
{
name: 'Batman'
};
}
console.log(foo());
Парсер подставит ';'
01.
02.
03.
04.
05.
06.
07.
32
33. И вообще советы
• Использовать Strict Mode
• Проверять код JShint
• Использовать IDE – WebStorm, например
• Boolean(x), Number(x), String(x) – для приведения типов
• Mozilla Developer Network
33