*****
var myObject = {
    a: 2
};

myObject.a; // 2
*****
var anotherObject = {
    a: 2
};

// Utworzenie obiektu poczonego z anotherObject.
var myObject = Object.create( anotherObject );

myObject.a; // 2
*****
var anotherObject = {
    a: 2
};

// Utworzenie obiektu poczonego z anotherObject.
var myObject = Object.create( anotherObject );

for (var k in myObject) {
    console.log("Znaleziono: " + k);
}
// Znaleziono: a

("a" in myObject); // Prawda.
*****
myObject.foo = "bar";
*****
var anotherObject = {
    a: 2
};
var myObject = Object.create( anotherObject );

anotherObject.a; // 2
myObject.a; // 2

anotherObject.hasOwnProperty( "a" ); // Prawda.
myObject.hasOwnProperty( "a" ); // Fasz.

myObject.a++; // Ups! Niejawne przesonicie!

anotherObject.a; // 2
myObject.a; // 3

myObject.hasOwnProperty( "a" ); // Prawda.
*****
function Foo() {
    // ...
}

Foo.prototype; // { }
*****
function Foo() {
    // ...
}

var a = new Foo();

Object.getPrototypeOf( a ) === Foo.prototype; // Prawda.
*****
function Foo() {
    // ...
}

var a = new Foo();
*****
function Foo() {
    // ...
}

Foo.prototype.constructor === Foo; // Prawda.

var a = new Foo();
a.constructor === Foo; // Prawda.
*****
function NothingSpecial() {
    console.log( "Wszystko mi jedno!" );
}

var a = new NothingSpecial();
// "Wszystko mi jedno!"

a; // {}
*****
function Foo(name) {
    this.name = name;
}

Foo.prototype.myName = function() {
    return this.name;
};

var a = new Foo( "a" );
var b = new Foo( "b" );

a.myName(); // "a"
b.myName(); // "b"
*****
function Foo() { /* .. */ }

Foo.prototype = { /* .. */ }; // Utworzenie nowego obiektu prototype.

var a1 = new Foo();
a1.constructor === Foo; // Fasz!
a1.constructor === Object; // Prawda!
*****
function Foo() { /* .. */ }

Foo.prototype = { /* .. */ }; // Utworzenie nowego obiektu prototype.

// Konieczne jest poprawne usunicie usterki, jak jest brak waciwoci
// '.constructor' w nowym obiekcie dostpnym jako 'Foo.prototype'.
// Zajrzyj do rozdziau 3. i poszukaj tam opisu funkcji defineProperty(..).
Object.defineProperty( Foo.prototype, "constructor" , {
    enumerable: false,
    writable: true,
    configurable: true,
    value: Foo    // Wskazuje waciwo '.constructor' w obiekcie 'Foo'.
} );
*****
function Foo(name) {
    this.name = name;
}

Foo.prototype.myName = function() {
    return this.name;
};

function Bar(name,label) {
    Foo.call( this, name );
    this.label = label;
}

// W tym miejscu tworzymy nowy obiekt Bar.prototype
// poczony z Foo.prototype.
Bar.prototype = Object.create( Foo.prototype );

// Uwaaj! Teraz nie ma ju Bar.prototype.constructor
// i moe wystpi konieczno rcznego poprawienia,
// jeeli masz w zwyczaju opieranie si na tego rodzaju waciwociach!

Bar.prototype.myLabel = function() {
    return this.label;
};

var a = new Bar( "a", "obj a" );

a.myName(); // "a"
a.myLabel(); // "obj a"
*****
// Ponisze polecenie nie dziaa tak, jak tego oczekujesz!
Bar.prototype = Foo.prototype;

// Ponisze polecenie mniej wicej dziaa w oczekiwany sposb,
// ale jednoczenie powoduje efekty uboczne, ktrych nie chcesz :(
Bar.prototype = new Foo();
*****
// Przed wprowadzeniem specyfikacji ES6.
// Pozbycie si domylnego, istniejcego obiektu Bar.prototype.
Bar.prototype = Object.create( Foo.prototype );

// Specyfikacja ES6+.
// Modyfikacja istniejcego obiektu Bar.prototype.
Object.setPrototypeOf( Bar.prototype, Foo.prototype );
*****
function Foo() {
    // ...
}

Foo.prototype.blah = ...;

var a = new Foo();
*****
a instanceof Foo; // Prawda.
*****
// Funkcja pomocnicza pozwalajca sprawdzi, czy obiekt 'o1'
// jest w jakikolwiek sposb powizany (delegowany) z 'o2'.
function isRelatedTo(o1, o2) {
    function F(){}
    F.prototype = o2;
    return o1 instanceof F;
}

var a = {};
var b = Object.create( a );

isRelatedTo( b, a ); // Prawda.
*****
Foo.prototype.isPrototypeOf( a ); // Prawda.
*****
// Sprawdzamy, czy 'b' pojawia si w jakimkolwiek
// miejscu acucha [[Prototype]] obiektu 'c'.
b.isPrototypeOf( c );
*****
Object.getPrototypeOf( a );
*****
Object.getPrototypeOf( a ) === Foo.prototype; // Prawda.
*****
a.__proto__ === Foo.prototype; // Prawda.
*****
Object.defineProperty( Object.prototype, "__proto__", {
    get: function() {
        return Object.getPrototypeOf( this );
    },
    set: function(o) {
        // Wywoanie setPrototypeOf(..) w specyfikacji ES6.
        Object.setPrototypeOf( this, o );
        return o;
    }
} );
*****
var foo = {
    something: function() {
        console.log( "Powiedz mi co dobrego..." );
    }
};

var bar = Object.create( foo );

bar.something(); // Powiedz mi co dobrego...
*****
if (!Object.create) {
    Object.create = function(o) {
        function F(){}
        F.prototype = o;
        return new F();
    };
}
*****
var anotherObject = {
    a: 2
};

var myObject = Object.create( anotherObject, {
    b: {
        enumerable: false,
        writable: true,
        configurable: false,
        value: 3
    },
    c: {
        enumerable: true,
        writable: false,
        configurable: false,
        value: 4
    }
} );

myObject.hasOwnProperty( "a" ); // Fasz.
myObject.hasOwnProperty( "b" ); // Prawda.
myObject.hasOwnProperty( "c" ); // Prawda.

myObject.a; // 2
myObject.b; // 3
myObject.c; // 4
*****
function createAndLinkObject(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

var anotherObject = {
    a: 2
};

var myObject = createAndLinkObject( anotherObject );

myObject.a; // 2
*****
var anotherObject = {
    cool: function() {
        console.log( "wietnie!" );
    }
};

var myObject = Object.create( anotherObject );

myObject.cool(); // "wietnie!"
*****
var anotherObject = {
    cool: function() {
        console.log( "wietnie!" );
    }
};

var myObject = Object.create( anotherObject );

myObject.doCool = function() {
    this.cool(); // Wewntrzne delegowanie!
};

myObject.doCool(); // "wietnie!"
*****

??





108	(	Rozdzia 5. Bd! W dokumencie nie ma tekstu o podanym stylu.

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



					107

