*****
class Widget {
    constructor(width,height) {
        this.width = width || 50;
        this.height = height || 50;
        this.$elem = null;
    }
    render($where){
        if (this.$elem) {
            this.$elem.css( {
                width: this.width + "px",
                height: this.height + "px"
            } ).appendTo( $where );
        }
    }
}

class Button extends Widget {
    constructor(width,height,label) {
        super( width, height );
        this.label = label || "Domylny";
        this.$elem = $( "<button>" ).text( this.label );
    }
    render($where) {
        super( $where );
        this.$elem.click( this.onClick.bind( this ) );
    }
    onClick(evt) {
        console.log( "Przycisk '" + this.label + "' zosta kliknity!" );
    }
}
*****
class C {
    constructor() {
        this.num = Math.random();
    }
    rand() {
        console.log( "Losowa liczba: " + this.num );
    }
}

var c1 = new C();
c1.rand(); // "Losowa liczba: 0.4324299..."

C.prototype.rand = function() {
    console.log( "Losowa liczba: " + Math.round( this.num * 1000 ));
};

var c2 = new C();
c2.rand(); // "Losowa liczba: 867"

c1.rand(); // "Losowa liczba: 432" -- Ups!!!
*****
class C {
    constructor() {
        // Upewniamy si co do modyfikacji wspdzielonego stanu,
        // a nie co do ustawienia przesaniajcej waciwoci
        // w egzemplarzach!
        C.prototype.count++;

        // W tym miejscu 'this.count' dziaa zgodnie z oczekiwaniami
        // za pomoc delegowania.
        console.log( "Witaj: " + this.count );
    }
}
// Dodanie waciwoci dla wspdzielonego stanu
// bezporednio do obiektu prototypu.
C.prototype.count = 0;

var c1 = new C();
// Witaj: 1

var c2 = new C();
// Witaj: 2

c1.count === 2; // Prawda.
c1.count === c2.count; // Prawda.
*****
class C {
    constructor(id) {
        // Ups, puapka! Przesaniamy metod id() 
        // wartoci waciwoci w egzemplarzu.
        this.id = id;
    }
    id() {
        console.log( "Id: " + id );
    }
}

var c1 = new C( "c1" );
c1.id(); // TypeError -- 'c1.id' to teraz cig tekstowy "c1".
*****
class P {
    foo() { console.log( "P.foo" ); }
}
class C extends P {
    foo() {
        super();
    }
}

var c1 = new C();
c1.foo(); // "P.foo"

var D = {
    foo: function() { console.log( "D.foo" ); }
};

var E = {
    foo: C.prototype.foo
};

// cze 'E' do 'D' zapewniajce obsug delegowania.
Object.setPrototypeOf( E, D );

E.foo(); // "P.foo"
*****
var D = {
    foo: function() { console.log( "D.foo" ); }
};

// cze 'E' do 'D' zapewniajce obsug delegowania.
var E = Object.create( D );

// Rczne doczenie [[HomeObject]] dla 'foo' jako
// 'E', E.[[Prototype]] wynosi 'D', wic 
// super() to D.foo().
E.foo = C.prototype.foo.toMethod( E, "foo" );

E.foo(); // "D.foo"

*****

??





174	(	Dodatek A Bd! W dokumencie nie ma tekstu o podanym stylu.

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



					173

