Bez oszukiwania – to znaczy, bez uruchamiania tego w przeglądarce ;)
Jaki bedzie wynik assert w poszczegolnych etapach kiedy wiadomosc jest logowana gdy condition jest false? Mozna podać pełne rozwiązanie lub zasadę :)
function assert(condition, message) {
    if(!condition) {
        console.error(message);
    }
}
assert(typeof fun === 'function', 'fun is not in scope');
assert(typeof a === 'number', 'a is not in scope');
assert(typeof b === 'number', 'b is not in scope');
assert(typeof c === 'number', 'c is not in scope');
function fun() {
    assert(typeof fun === 'function', 'IN_FUN: fun is not in scope');
    assert(typeof a === 'number', 'IN_FUN: a is not in scope');
    assert(typeof b === 'number', 'IN_FUN: b is not in scope');
    assert(typeof c === 'number', 'IN_FUN: c is not in scope');
    var b = 0;
    
    assert(typeof fun === 'function', 'AFTER_B: fun is not in scope');
    assert(typeof a === 'number', 'AFTER_B: a is not in scope');
    assert(typeof b === 'number', 'AFTER_B: b is not in scope');
    assert(typeof c === 'number', 'AFTER_B: c is not in scope');
    
    if(true) {
        assert(typeof fun === 'function', 'IN_IF_IN_FUN: fun is not in scope');
        assert(typeof a === 'number', 'IN_IF_IN_FUN: a is not in scope');
        assert(typeof b === 'number', 'IN_IF_IN_FUN: b is not in scope');
        assert(typeof c === 'number', 'IN_IF_IN_FUN: c is not in scope');
        var c = 0;
    }
    
    assert(typeof fun === 'function', 'AFTER_IF_IN_FUN: fun is not in scope');
    assert(typeof a === 'number', 'AFTER_IF_IN_FUN: a is not in scope');
    assert(typeof b === 'number', 'AFTER_IF_IN_FUN: b is not in scope');
    assert(typeof c === 'number', 'AFTER_IF_IN_FUN: c is not in scope');
}
assert(typeof fun === 'function', 'AFTER_FUN: fun is not in scope');
assert(typeof a === 'number', 'AFTER_FUN: a is not in scope');
assert(typeof b === 'number', 'AFTER_FUN: b is not in scope');
assert(typeof c === 'number', 'AFTER_FUN: c is not in scope');
var a = 0;
assert(typeof fun === 'function', 'AFTER_A: fun is not in scope');
assert(typeof a === 'number', 'AFTER_A: a is not in scope');
assert(typeof b === 'number', 'AFTER_A: b is not in scope');
assert(typeof c === 'number', 'AFTER_A: c is not in scope');
fun();
assert(typeof fun === 'function', 'AT_THE_END: fun is not in scope');
assert(typeof a === 'number', 'AT_THE_END: a is not in scope');
assert(typeof b === 'number', 'AT_THE_END: b is not in scope');
assert(typeof c === 'number', 'AT_THE_END: c is not in scope');
By kod może był czytelniejszy, tak to wygląda bez assert – //test –> 4 linijki assert dla fun, a, b i c
// test
function fun() {
    // test
    var b = 0;
    // test
    if(true) {
        // test
        var c = 0;
    }
    // test
}
// test
var a = 0;
// test
fun();
// test
    










 
        



Nie jestem mistrzem JS'a, ale wydaje mi się, że zasada wygląda tak:
1. Wszystkie definicje zmiennych wędrują na początek danego scope'a, tyle, że są undefined aż do momentu przypisania zmiennej.
2. Funkcja będzie zawsze widoczna (zdefiniowana) niezależnie od scope'a.
3. Jeżeli w scope istnieje zmienna o nazwie takiej samej jak zmienna ze scope'a nadrzędnego to przesłania ona tą zmienną z uwzględnieniem pkt. 1.
4. Zmienne są widoczne tylko "w dół" – w aktualnym scope i nested scope. Nie odwrotnie. Zmienne z wew. scope'a nie są widoczne na zewnątrz.
5. Tutaj scope to global i fun. If(true).. to nie scope (tylko kawałek scope'a fun)
Czyli patrząc na kod:
// test – 1,0,0,0
function fun() {
// test 1,1,0,0
var b = 0;
// test 1,1,1,0
if(true) {
// test 1,1,1,0
var c = 0;
}
// test – 1,1,1,1
}
// test 1,0,0,0
var a = 0;
// test 1,1,0,0
fun();
// test 1,1,0,0
Jestem w trakcie lektury JS: The Definitive Guide 6th, ale nie wykluczone, że coś pomieszałem w tym przeklętym przez Boga i ludzi języku ;)
Mogłeś zapodać b = 0 zamiast var b, to by było bardziej podchwytliwe :D
nie, all ok, GRATKI :) wlasnie o to chodzilo :) -> ze func sa dostepne globalnie zas zmienna dopiero po tym jak zostana zadeklarowane. dosc ciekawa ogolnie, do tego mozna byloby sie jeszcze dopytac dlaczego powtarzam kod testu a nie zamknalem go w funkcji ;) to by troche prace nad scope poprawilo
co do b, ciut by utrudnilo, ale nie az tak. w koncu this w fun jest window
Comments are closed.