*****
class Task {
    id;

    // Konstruktor Task().
    Task(ID) { id = ID; }
    outputTask() { output( id ); }
}

class XYZ inherits Task {
    label;

    // Konstruktor XYZ().
    XYZ(ID,Label) { super( ID ); label = Label; }
    outputTask() { super(); output( label ); }
}

class ABC inherits Task {
    // ...
}
*****
Task = {
    setID: function(ID) { this.id = ID; },
    outputID: function() { console.log( this.id ); }
};

// Obiekt XYZ deleguje do obiektu Task.
XYZ = Object.create( Task );

XYZ.prepareTask = function(ID,Label) {
    this.setID( ID );
    this.label = Label;
};

XYZ.outputTaskDetails = function() {
    this.outputID();
    console.log( this.label );
};

// ABC = Object.create( Task );
// ABC... = ...
*****
function Foo() {}

var a1 = new Foo();

a1; // Foo {}
*****
function Foo() {}

var a1 = new Foo();

a1.constructor; // Foo(){}
a1.constructor.name; // "Foo"
*****
function Foo() {}

var a1 = new Foo();

Foo.prototype.constructor = function Gotcha(){};

a1.constructor; // Gotcha(){}
a1.constructor.name; // "Gotcha"

a1; // Foo {}
*****
var Foo = {};

var a1 = Object.create( Foo );

a1; // Object {}

Object.defineProperty( Foo, "constructor", {
    enumerable: false,
    value: function Gotcha(){}
});

a1; // Gotcha {}
*****
function Foo(who) {
    this.me = who;
}
Foo.prototype.identify = function() {
    return "Jestem " + this.me;
};

function Bar(who) {
    Foo.call( this, who );
}
Bar.prototype = Object.create( Foo.prototype );

Bar.prototype.speak = function() {
    alert( "Witaj, " + this.identify() + "!" );
};

var b1 = new Bar( "b1" );
var b2 = new Bar( "b2" );

b1.speak();
b2.speak();
*****
Foo = {
    init: function(who) {
        this.me = who;
    },
    identify: function() {
        return "Jestem " + this.me;
    }
};

Bar = Object.create( Foo );

Bar.speak = function() {
    alert( "Witaj, " + this.identify() + "!" );
};

var b1 = Object.create( Bar );
b1.init( "b1" );
var b2 = Object.create( Bar );
b2.init( "b2" );

b1.speak();
b2.speak();
*****
// Klasa nadrzdna.
function Widget(width,height) {
    this.width = width || 50;
    this.height = height || 50;
    this.$elem = null;
}

Widget.prototype.render = function($where){
    if (this.$elem) {
        this.$elem.css( {
            width: this.width + "px",
            height: this.height + "px"
        } ).appendTo( $where );
    }
};

// Klasa potomna.
function Button(width,height,label) {
    // Wywoanie konstruktora "super".
    Widget.call( this, width, height );
    this.label = label || "Domylny";

    this.$elem = $( "<button>" ).text( this.label );
}

// Klasa 'Button' "dziedziczy" po klasie 'Widget'.
Button.prototype = Object.create( Widget.prototype );
// Nadpisanie bazowej "odziedziczonej" metody render(..).
Button.prototype.render = function($where) {
    // Wywoanie "super".
    Widget.prototype.render.call( this, $where );
    this.$elem.click( this.onClick.bind( this ) );
};

Button.prototype.onClick = function(evt) {
    console.log( "Przycisk '" + this.label + "' zosta kliknity!" );
};
$( document ).ready( function(){
    var $body = $( document.body );
    var btn1 = new Button( 125, 30, "Witaj," );
    var btn2 = new Button( 150, 40, "wiecie" );

    btn1.render( $body );
    btn2.render( $body );
} );
*****
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!" );
    }
}

$( document ).ready( function(){
    var $body = $( document.body );
    var btn1 = new Button( 125, 30, "Witaj," );
    var btn2 = new Button( 150, 40, "wiecie" );

    btn1.render( $body );
    btn2.render( $body );
} );
*****
var Widget = {
    init: function(width,height){
        this.width = width || 50;
        this.height = height || 50;
        this.$elem = null;
    },
    insert: function($where){
        if (this.$elem) {
            this.$elem.css( {
                width: this.width + "px",
                height: this.height + "px"
            } ).appendTo( $where );
        }
    }
};

var Button = Object.create( Widget );

Button.setup = function(width,height,label){
    // Delegowane wywoanie.
    this.init( width, height );
    this.label = label || "Domylny";

    this.$elem = $( "<button>" ).text( this.label );
};
Button.build = function($where) {
    // Delegowane wywoanie.
    this.insert( $where );
    this.$elem.click( this.onClick.bind( this ) );
};
Button.onClick = function(evt) {
    console.log( "Przycisk '" + this.label + "' zosta kliknity!" );
};

$( document ).ready( function(){
    var $body = $( document.body );

    var btn1 = Object.create( Button );
    btn1.setup( 125, 30, "Witaj," );

    var btn2 = Object.create( Button );
    btn2.setup( 150, 40, "wiecie" );

    btn1.build( $body );
    btn2.build( $body );
} );
*****
// Klasa nadrzdna.
function Controller() {
    this.errors = [];
}
Controller.prototype.showDialog = function(title,msg) {
    // Wywietlenie uytkownikowi tytuu i wiadomoci w oknie dialogowym.
};
Controller.prototype.success = function(msg) {
    this.showDialog( "Sukces", msg );
};
Controller.prototype.failure = function(err) {
    this.errors.push( err );
    this.showDialog( "Bd", err );
};
// Klasa potomna.
function LoginController() {
    Controller.call( this );
}
// Poczenie klasy potomnej z nadrzdn.
LoginController.prototype =
    Object.create( Controller.prototype );
LoginController.prototype.getUser = function() {
    return document.getElementById( "login_username" ).value;
};
LoginController.prototype.getPassword = function() {
    return document.getElementById( "login_password" ).value;
};
LoginController.prototype.validateEntry = function(user,pw) {
    user = user || this.getUser();
    pw = pw || this.getPassword();

    if (!(user && pw)) {
        return this.failure(
           "Prosz poda nazw uytkownika i haso!"
        );
    }
    else if (pw.length < 5) {
        return this.failure(
           "Haso musi zawiera przynajmniej 5 znakw!"
        );
    }

    // Dotare tutaj? Zostae uwierzytelniony!
    return true;
};
// Nadpisanie w celu rozszerzenia metody bazowej failure().
LoginController.prototype.failure = function(err) {
    // Wywoanie "super".
    Controller.prototype.failure.call(
       this,
       "Logowanie nieprawidowe: " + err
    );
};
// Klasa potomna.
function AuthController(login) {
    Controller.call( this );
    // Poza dziedziczeniem potrzebujemy rwnie kompozycji.
    this.login = login;
}
// Poczenie klasy potomnej z nadrzdn.
AuthController.prototype =
   Object.create( Controller.prototype );
AuthController.prototype.server = function(url,data) {
    return $.ajax( {
        url: url,
        data: data
    } );
};
AuthController.prototype.checkAuth = function() {
    var user = this.login.getUser();
    var pw = this.login.getPassword();
    if (this.login.validateEntry( user, pw )) {
        this.server( "/check-auth",{
            user: user,
            pw: pw
        } )
        .then( this.success.bind( this ) )
        .fail( this.failure.bind( this ) );
    }
};
// Nadpisanie w celu rozszerzenia metody bazowej success().
AuthController.prototype.success = function() {
    // Wywoanie "super".
    Controller.prototype.success.call( this, "Uwierzytelniony!" );
};
// Nadpisanie w celu rozszerzenia metody bazowej failure().
AuthController.prototype.failure = function(err) {
    // Wywoanie "super".
    Controller.prototype.failure.call(
       this,
       "Uwierzytelnienie nieprawidowe: " + err
    );
};
var auth = new AuthController(
    // Poza dziedziczeniem potrzebujemy rwnie kompozycji.
    new LoginController()
);
auth.checkAuth();
*****
var LoginController = {
    errors: [],
    getUser: function() {
        return document.getElementById(
           "login_username"
        ).value;
    },
    getPassword: function() {
        return document.getElementById(
           "login_password"
        ).value;
    },
    validateEntry: function(user,pw) {
        user = user || this.getUser();
        pw = pw || this.getPassword();

        if (!(user && pw)) {
            return this.failure(
               "Prosz poda nazw uytkownika i haso!"
            );
        }
        else if (pw.length < 5) {
            return this.failure(
               "Haso musi zawiera przynajmniej 5 znakw!"
            );
        }

        // Dotare tutaj? Zostae uwierzytelniony!
        return true;
    },
    showDialog: function(title,msg) {
        // Wywietlenie uytkownikowi komunikatu sukcesu w oknie dialogowym
    },
    failure: function(err) {
        this.errors.push( err );
        this.showDialog( "Bd", "Logowanie nieprawidowe: " + err );
    }
};

// Obiekt AuthController zostaje poczony z LoginController i deleguje do niego.
var AuthController = Object.create( LoginController );

AuthController.errors = [];
AuthController.checkAuth = function() {
    var user = this.getUser();
    var pw = this.getPassword();
    if (this.validateEntry( user, pw )) {
        this.server( "/check-auth",{
            user: user,
            pw: pw
        } )
        .then( this.accepted.bind( this ) )
        .fail( this.rejected.bind( this ) );
    }
};
AuthController.server = function(url,data) {
    return $.ajax( {
        url: url,
        data: data
    } );
};
AuthController.accepted = function() {
    this.showDialog( "Sukces", "Uwierzytelniony!" )
};
AuthController.rejected = function(err) {
    this.failure( "Uwierzytelnienie nieprawidowe: " + err );
};
*****
AuthController.checkAuth();
*****
var controller1 = Object.create( AuthController );
var controller2 = Object.create( AuthController );
*****
class Foo {
    methodName() { /* .. */ }
}
*****
var LoginController = {
    errors: [],
    getUser() { // Zauwa brak sowa kluczowego 'function'!
        // ...
    },
    getPassword() {
        // ...
    }
    // ...
};
*****
// Uycie adniejszej skadni literau obiektu wraz ze zwizymi metodami!
var AuthController = {
    errors: [],
    checkAuth() {
        // ...
    },
    server(url,data) {
        // ...
    }
    // ...
};
// TERAZ czymy AuthController z LoginController.
Object.setPrototypeOf( AuthController, LoginController );
*****
var Foo = {
    bar() { /*..*/ },
    baz: function baz() { /*..*/ }
};
*****
var Foo = {
    bar: function() { /*..*/ },
    baz: function baz() { /*..*/ }
};
*****
var Foo = {
    bar: function(x) {
        if (x < 10) {
            return Foo.bar( x * 2 );
        }
        return x;
    },
    baz: function baz(x) {
        if (x < 10) {
            return baz( x * 2 );
        }
        return x;
    }
};
*****
function Foo() {
    // ...
}
Foo.prototype.something = function(){
    // ...
}

var a1 = new Foo();

// Pniej.

if (a1 instanceof Foo) {
    a1.something();
}
*****
function Foo() { /* .. */ }
Foo.prototype...

function Bar() { /* .. */ }
Bar.prototype = Object.create( Foo.prototype );

var b1 = new Bar( "b1" );
*****
// Powizanie obiektw 'Foo' i 'Bar' ze sob.
Bar.prototype instanceof Foo; // Prawda.
Object.getPrototypeOf( Bar.prototype )
   === Foo.prototype; // Prawda.
Foo.prototype.isPrototypeOf( Bar.prototype ); // Prawda.

// Powizanie 'b'1 zarwno z 'Foo', jak i z 'Bar'.
b1 instanceof Foo; // Prawda.
b1 instanceof Bar; // Prawda.
Object.getPrototypeOf( b1 ) === Bar.prototype; // Prawda.
Foo.prototype.isPrototypeOf( b1 ); // Prawda.
Bar.prototype.isPrototypeOf( b1 ); // Prawda.
*****
if (a1.something) {
    a1.something();
}
*****
var Foo = { /* .. */ };

var Bar = Object.create( Foo );
Bar...

var b1 = Object.create( Bar );
*****
// Powizanie obiektw 'Foo' i 'Bar' ze sob.
Foo.isPrototypeOf( Bar ); // Prawda.
Object.getPrototypeOf( Bar ) === Foo; // Prawda.

// Powizanie 'b1' zarwno z 'Foo', jak i z 'Bar'.
Foo.isPrototypeOf( b1 ); // Prawda.
Bar.isPrototypeOf( b1 ); // Prawda.
Object.getPrototypeOf( b1 ) === Bar; // Prawda.
*****

??





140	(	Rozdzia 6. Bd! W dokumencie nie ma tekstu o podanym stylu.

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



					139

