*****
function baz() {
    // Stos wywoa to: 'baz', wic rdo wywoania
    // funkcji znajduje si w zakresie globalnym.

    console.log( "baz" );
    bar(); // <-- rdo wywoania funkcji 'bar'.
}

function bar() {
    // Stos wywoa to: 'baz' -> 'bar', wic rdo wywoania
    // funkcji znajduje si w 'baz'.

    console.log( "bar" );
    foo(); // <-- rdo wywoania funkcji 'foo'.
}

function foo() {
    // Stos wywoa to: 'baz' -> 'bar' -> 'foo',
    // wic rdo wywoania funkcji znajduje si w 'bar'.

    console.log( "foo" );
}

baz(); // <-- rdo wywoania funkcji 'baz'.
*****
function foo() {
    console.log( this.a );
}

var a = 2;

foo(); // 2
*****
function foo() {
    "use strict";

    console.log( this.a );
}

var a = 2;

foo(); // TypeError: 'this' odwouje si do wartoci 'undefined'.
*****
function foo() {
    console.log( this.a );
}

var a = 2;

(function(){
    "use strict";
    foo(); // 2
})();
*****
function foo() {
    console.log( this.a );
}

var obj = {
    a: 2,
    foo: foo
};

obj.foo(); // 2
*****
function foo() {
    console.log( this.a );
}

var obj2 = {
    a: 42,
    foo: foo
};

var obj1 = {
    a: 2,
    obj2: obj2
};

obj1.obj2.foo(); // 42
*****
function foo() {
    console.log( this.a );
}

var obj = {
    a: 2,
    foo: foo
};

var bar = obj.foo; // Odwoanie do funkcji/alias!
var a = "oops, global"; // 'a' jest rwnie waciwoci w obiekcie globalnym.

bar(); // "Ups! Obiekt globalny".
*****
function foo() {
    console.log( this.a );
}

function doFoo(fn) {
    // 'fn' to po prostu kolejne odwoanie do 'foo'.

    fn(); // <-- rdo wywoania funkcji!
}

var obj = {
    a: 2,
    foo: foo
};

var a = "oops, global"; // 'a' jest rwnie waciwoci w obiekcie globalnym.

doFoo( obj.foo ); // "Ups! Obiekt globalny".
*****
function foo() {
    console.log( this.a );
}

var obj = {
    a: 2,
    foo: foo
};

var a = "oops, global"; // 'a' jest rwnie waciwoci w obiekcie globalnym.

setTimeout( obj.foo, 100 ); // "Ups! Obiekt globalny".
*****
function setTimeout(fn, delay) {
    // Oczekiwanie przez podan (warto 'delay') liczb milisekund.
    fn(); // <-- rdo wywoania funkcji!
}
*****
function foo() {
    console.log( this.a );
}

var obj = {
    a: 2
};

foo.call( obj ); // 2
*****
function foo() {
    console.log( this.a );
}

var obj = {
    a: 2
};

var bar = function() {
    foo.call( obj );
};

bar(); // 2
setTimeout( bar, 100 ); // 2

// W przypadku trwaego wizania 'bar' nie bdziemy mieli do czynienia z nadpisywaniem 'this'.
bar.call( window ); // 2
*****
function foo(something) {
    console.log( this.a, something );
    return this.a + something;
}

var obj = {
    a: 2
};

var bar = function() {
    return foo.apply( obj, arguments );
};

var b = bar( 3 ); // 2 3
console.log( b ); // 5
*****
function foo(something) {
    console.log( this.a, something );
    return this.a + something;
}

// Prosta funkcja pomocnicza bind().
function bind(fn, obj) {
    return function() {
        return fn.apply( obj, arguments );
    };
}

var obj = {
    a: 2
};
var bar = bind( foo, obj );

var b = bar( 3 ); // 2 3
console.log( b ); // 5
*****
function foo(something) {
    console.log( this.a, something );
    return this.a + something;
}

var obj = {
    a: 2
};

var bar = foo.bind( obj );

var b = bar( 3 ); // 2 3
console.log( b ); // 5
*****
function foo(el) {
    console.log( el, this.id );
}

var obj = {
    id: "wspaniale"
};

// Uycie 'obj' jako 'this' dla wywoa 'foo(..)'.
[1, 2, 3].forEach( foo, obj );
// 1 wspaniale 2 wspaniale 3 wspaniale
*****
something = new MyClass(..);
*****
function foo(a) {
    this.a = a;
}

var bar = new foo( 2 );
console.log( bar.a ); // 2
*****
function foo() {
    console.log( this.a );
}

var obj1 = {
    a: 2,
    foo: foo
};

var obj2 = {
    a: 3,
    foo: foo
};

obj1.foo(); // 2
obj2.foo(); // 3

obj1.foo.call( obj2 ); // 3
obj2.foo.call( obj1 ); // 2
*****
function foo(something) {
    this.a = something;
}

var obj1 = {
    foo: foo
};

var obj2 = {};

obj1.foo( 2 );
console.log( obj1.a ); // 2

obj1.foo.call( obj2, 3 );
console.log( obj2.a ); // 3

var bar = new obj1.foo( 4 );
console.log( obj1.a ); // 2
console.log( bar.a ); // 4
*****
function foo(something) {
    this.a = something;
}

var obj1 = {};

var bar = foo.bind( obj1 );
bar( 2 );
console.log( obj1.a ); // 2

var baz = new bar( 3 );
console.log( obj1.a ); // 2
console.log( baz.a ); // 3
*****
function bind(fn, obj) {
    return function() {
        fn.apply( obj, arguments );
    };
}
*****
if (!Function.prototype.bind) {
    Function.prototype.bind = function(oThis) {
        if (typeof this !== "function") {
            // Rozwizanie najblisze specyfikacji ECMAScript 5
            // to wewntrzna funkcja IsCallable.
            throw new TypeError(
               "Function.prototype.bind - nastpia prba wywoania " +
               "tego, czego nie mona wywoa."
            );
        }

        var aArgs = Array.prototype.slice.call( arguments, 1 ),
            fToBind = this,
            fNOP = function(){},
            fBound = function(){
                return fToBind.apply(
                    (
                        this instanceof fNOP &&
                        oThis ? this : oThis
                    ),
                    aArgs.concat(
                       Array.prototype.slice.call( arguments )
                    );

            }
        ;
        fNOP.prototype = this.prototype;
        fBound.prototype = new fNOP();
        return fBound;
    };
}
*****
this instanceof fNOP &&
oThis ? this : oThis

// ...i:

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
*****
function foo(p1,p2) {
        this.val = p1 + p2;
}

// Poniej uywamy 'null', poniewa nie przejmujemy si
// 'this' w wizaniu twardym w przedstawionym przypadku.
// Poza tym i tak dojdzie do nadpisania 'this' przez 'new'!
var bar = foo.bind( null, "p1" );

var baz = new bar( "p2" );

baz.val; // p1p2
*****
var bar = new foo()
*****
var bar = foo.call( obj2 )
*****
var bar = obj1.foo()
*****
var bar = foo()
*****
function foo() {
    console.log( this.a );
}

var a = 2;

foo.call( null ); // 2
*****
function foo(a,b) {
    console.log( "a:" + a + ", b:" + b );
}

// Przekazanie tablicy jako parametrw.
foo.apply( null, [2, 3] ); // a:2, b:3

// Przekazanie danych za pomoc 'bind(..)'.
var bar = foo.bind( null, 2 );
bar( 3 ); // a:2, b:3
*****
function foo(a,b) {
    console.log( "a:" + a + ", b:" + b );
}

// Nasz pusty obiekt DMZ.
var o = Object.create( null );

// Przekazanie tablicy jako parametrw.
foo.apply( o, [2, 3] ); // a:2, b:3

// Rozwinicie funkcji za pomoc wywoania 'bind(..)'.
var bar = foo.bind( o, 2 );
bar( 3 ); // a:2, b:3
*****
function foo() {
    console.log( this.a );
}

var a = 2;
var o = { a: 3, foo: foo };
var p = { a: 4 };

o.foo(); // 3
(p.foo = o.foo)(); // 2
*****
if (!Function.prototype.softBind) {
    Function.prototype.softBind = function(obj) {
        var fn = this,
            curried = [].slice.call( arguments, 1 ),
            bound = function bound() {
                return fn.apply(
                    (!this ||
                        (typeof window !== "undefined" &&
                            this === window) ||
                        (typeof global !== "undefined" &&
                            this === global)
                    ) ? obj : this,
                    curried.concat.apply( curried, arguments )
                );
            };
        bound.prototype = Object.create( fn.prototype );
        return bound;
    };
}
*****
function foo() {
   console.log("nazwa: " + this.name);
}

var obj = { name: "obj" },
    obj2 = { name: "obj2" },
    obj3 = { name: "obj3" };

var fooOBJ = foo.softBind( obj );

fooOBJ(); // nazwa: obj

obj2.foo = foo.softBind(obj);
obj2.foo(); // nazwa: obj2   <---- spjrz!!!

fooOBJ.call( obj3 ); // nazwa: obj3   <---- spjrz!

setTimeout( obj2.foo, 10 );
// nazwa: obj   <---- rozwizanie awaryjne polegajce na uyciu wizania mikkiego.
*****
function foo() {
    // Zwrot funkcji typu arrow function.
    return (a) => {
        // Tutaj 'this' pod wzgldem leksykalnym dziedziczy po 'foo()'.
        console.log( this.a );
    };
}

var obj1 = {
    a: 2
};

var obj2 = {
    a: 3
};

var bar = foo.call( obj1 );
bar.call( obj2 ); // 2, nie 3!
*****
function foo() {
    setTimeout(() => {
        // Tutaj 'this' pod wzgldem leksykalnym dziedziczy po 'foo()'.
        console.log( this.a );
    },100);
}

var obj = {
    a: 2
};

foo.call( obj ); // 2
*****
function foo() {
    var self = this; // Leksykalne przechwycenie 'this'.
    setTimeout( function(){
        console.log( self.a );
    }, 100 );
}

var obj = {
    a: 2
};

foo.call( obj ); // 2
*****
??





24	(	Rozdzia 2. Bd! W dokumencie nie ma tekstu o podanym stylu.

			Bd! W dokumencie nie ma tekstu o podanym stylu.	(	33



					23

