Constructor.prototype.method = function () {
setTimeout(function () {
this.anotherMethod();
}, 5000);
};
Constructor.prototype.anotherMethod = function () { };
Constructor.prototype.method = function () {
setTimeout(function () {
this.anotherMethod();
}, 5000);
};
Constructor.prototype.anotherMethod = function () { };
Constructor.prototype.method = function () {
var self = this;
setTimeout(function () {
self.anotherMethod();
}, 5000);
};
Constructor.prototype.anotherMethod = function () { };
Constructor.prototype.method = function () {
setTimeout(function () {
this.anotherMethod();
}.bind(this), 5000);
};
Constructor.prototype.anotherMethod = function () { };
var index = 123, arr = ['tro', 'lo', 'lo'];
for (var index = 0; index < arr.length; index++) {
arr[index] += 'o';
}
console.log(index);
Output: 123
var index = 123, arr = ['tro', 'lo', 'lo'];
for (var index = 0; index < arr.length; index++) {
arr[index] += 'o';
}
console.log(index);
Output: 3
var obj = {some: 'first', another: 'second'};
for(var key in obj) {
setTimeout(function () {
console.log(obj[key]);
}, 1000);
}
Output: first second
var obj = {some: 'first', another: 'second'};
for(var key in obj) {
setTimeout(function () {
console.log(obj[key]);
}, 1000);
}
Output: second second
var obj = {some: 'first', another: 'second'};
for(var key in obj) {
(function (currentKey){
setTimeout(function () {
console.log(obj[currentKey]);
}, 1000);
})(key);
}
var obj = {some: 'first', another: 'second'};
Object.keys(obj)
.forEach(function (key) {
setTimeout(function () {
console.log(obj[key]);
}, 1000);
});
var some = 'cool and not ';
function foo() {
console.log(some);
var some = 'awful!';
console.log(some);
}
foo();
Output: cool and not awful!
var some = 'cool and not ';
function foo() {
console.log(some); // some === undefined
var some = 'awful!';
console.log(some);
}
foo();
Output: undefined awful!
for (var index = 0; index < 1000000; index++) {
console.log(index);
}
for (var index = 0; index < 1000000; index++) {
console.log(index);
}
Страница не обрабатывает события
Сервер не принимает новые подключения
var index = 0;
function next() {
if (index >= 1000000) { return; }
console.log(index++);
setTimeout(next, 0);
}
next();
var arr = [];
arr[5] = 'some';
console.log(arr.length);
Output: 1
var arr = [];
arr[5] = 'some';
console.log(arr.length);
Output: 6Использовать push/unshift/pop/shift
var arr = [];
arr[5] = 'some';
delete arr[5];
console.log(arr.length);
Output: 0
var arr = [];
arr[5] = 'some';
delete arr[5];
console.log(arr.length);
Output: 6Не использовать delete
function foo () {
return
{
name: 'Batman'
};
}
console.log(foo());
function foo () {
return
{
name: 'Batman'
};
}
console.log(foo());
Парсер подставит ';'