referencje, closure, this
Bartosz Szczeciński
16.05.2018
@btmpl
medium.com/@baphemot
var test = 1;
var test = 2;
var test = 3;
test = 4;
test = 5;
console.log(test);5var test = 1;
function testFunction() {
console.log('testFunction', test);
}
console.log('global', test);
testFunction();
console.log('global', test);global 1
testFunction 1
global 1var test = 1;
function testFunction() {
var test = 2;
console.log('testFunction', test);
}
console.log('global', test);
testFunction();
console.log('global', test);global 1
testFunction 2
global 1var test = 1;
function testFunction() {
test = 2;
console.log('testFunction', test);
}
console.log('global', test);
testFunction();
console.log('global', test);global 1
testFunction 2
global 2Jeżeli pominiemy var przy odczycie, parser sprawdzi czy zmienna jest już zadeklarowana, jeżeli nie, sprawdzi w zakresie wyżej (w tym przypadku - window)
Jeżeli pominiemy specyfikator var przy zapisie parser uznaje, że zmienna została już zadeklarowała i "szuka jej". Jeżeli nie znajdzie jej w zakresie, szuka jej w zakresie wyżej. Jeżeli nie znajdzie, tworzy lub zmienia na zakresie globalnym window.
var test = 1;
function testFunction() {
var test = 2;
console.log('testFunction', test);
}
console.log('global', test);
setTimeout(testFunction, 1000);
console.log('global', test);global 1
global 1
testFunction 2console.clear();
var test = 1;
function testFunction() {
console.log('testFunction', test);
}
console.log('global', test);
setTimeout(testFunction, 1000);
console.log('global', test);global 1
global 1
testFunction 1console.clear();
var test = 1;
function testFunction() {
console.log('testFunction', test);
}
console.log('global', test);
setTimeout(testFunction, 1000);
console.log('global', test);
test = 5;global 1
global 1
testFunction 5console.clear();
var test = 1;
console.log('global', test);
function zrobZakres() {
var mojaZmienna = test;
function testFunction() {
console.log('testFunction', mojaZmienna);
}
setTimeout(testFunction, 1000);
}
zrobZakres();
console.log('global', test);
test = 5;global 1
global 1
testFunction 1console.clear();
var test = 1;
console.log('global', test);
(function() {
var mojaZmienna = test;
function testFunction() {
console.log('testFunction', mojaZmienna);
}
setTimeout(testFunction, 1000);
})();
console.log('global', test);
test = 5;global 1
global 1
testFunction 1var person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
person.getName();Bartekvar person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
person.getName.call();undefinedFunkcje .call i .apply jako pierwszy parametr przekazują this do wywoływanej funkcji. Jeżeli go nie przekażemy, podstawiony zostanie this z momentu wywołania (window)
var imie = 'Bartosz';
var person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
person.getName.call();BartoszIstnieje window.imie, więc window podstawione jako this posiada zmienną this.imie
var person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
person.getName.call(person);Bartekvar person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
var kopiaFunkcji = person.getName;
kopiaFunkcji();undefinedFunkcja kopiowana jest przez referencję, co w praktyce oznacza "wykonaj kopię ciała funkcji"
var person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
var otherPerson = {
imie: 'Marcin'
}
var kopiaFunkcji = person.getName;
kopiaFunkcji.call(otherPerson);Marcinvar person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
setTimeout(person.getName, 1000);undefinedvar person = {
imie: 'Bartek',
getName: function() {
console.log(this.imie);
}
}
setTimeout(person.getName.bind(person), 1000);Bartek.bind pozwala na trwałe przypisanie jakie this powinno zostać przekazane do funkcji