*****
var myObj = {
    key: value
    // ...
};
*****
var myObj = new Object();
myObj.key = value;
*****
var strPrimitive = "To jest cig tekstowy.";
typeof strPrimitive; // "string"
strPrimitive instanceof String; // Fasz.

var strObject = new String( "To jest cig tekstowy." );
typeof strObject; // "object"
strObject instanceof String; // Prawda.

// Sprawdzenie podtypu obiektu.
Object.prototype.toString.call( strObject ); // [object String]
*****
var strPrimitive = "To jest cig tekstowy.";

console.log( strPrimitive.length ); // 22

console.log( strPrimitive.charAt( 3 ) ); // "j"
*****
var myObject = {
    a: 2
};

myObject.a; // 2

myObject["a"]; // 2
*****
var myObject = {
    a: 2
};

var idx;
if (wantA) {
    idx = "a";
}

// Pniej.

console.log( myObject[idx] ); // 2
*****
var myObject = { };

myObject[true] = "foo";
myObject[3] = "bar";
myObject[myObject] = "baz";

myObject["true"]; // "foo"
myObject["3"]; // "bar"
myObject["[object Object]"]; // "baz"
*****
var prefix = "foo";

var myObject = {
    [prefix + "bar"]: "Witaj,",
    [prefix + "baz"]: "wiecie!"
};

myObject["foobar"]; // Witaj,
myObject["foobaz"]; // wiecie!
*****
var myObject = {
    [Symbol.Something]: "Witaj, wiecie!"
};
*****
function foo() {
    console.log( "foo" );
}

var someFoo = foo; // Odwoanie zmiennej do 'foo'.

var myObject = {
    someFoo: foo
};

foo; // function foo(){..}

someFoo; // function foo(){..}

myObject.someFoo; // function foo(){..}
*****
var myObject = {
    foo: function foo() {
        console.log( "foo" );
    }
};

var someFoo = myObject.foo;

someFoo; // function foo(){..}

myObject.foo; // function foo(){..}
*****
var myArray = [ "foo", 42, "bar" ];

myArray.length; // 3

myArray[0]; // "foo"

myArray[2]; // "bar"
*****
var myArray = [ "foo", 42, "bar" ];

myArray.baz = "baz";

myArray.length; // 3

myArray.baz; // "baz"
*****
var myArray = [ "foo", 42, "bar" ];

myArray["3"] = "baz";

myArray.length; // 4

myArray[3]; // "baz"
*****
function anotherFunction() { /*..*/ }

var anotherObject = {
    c: true
};

var anotherArray = [];

var myObject = {
    a: 2,
    b: anotherObject, // Odwoanie, a nie kopia!
    c: anotherArray, // Inne odwoanie!
    d: anotherFunction
};

anotherArray.push( anotherObject, myObject );
*****
var newObj = JSON.parse( JSON.stringify( someObj ) );
*****
var newObj = Object.assign( {}, myObject );

newObj.a; // 2
newObj.b === anotherObject; // Prawda.
newObj.c === anotherArray; // Prawda.
newObj.d === anotherFunction; // Prawda.

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

Object.getOwnPropertyDescriptor( myObject, "a" );
// {
//    value: 2,
//    writable: true,
//    enumerable: true,
//    configurable: true
// }
*****
var myObject = {};

Object.defineProperty( myObject, "a", {
    value: 2,
    writable: true,
    configurable: true,
    enumerable: true
} );

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

Object.defineProperty( myObject, "a", {
    value: 2,
    writable: false, // Brak moliwoci zmiany wartoci!
    configurable: true,
    enumerable: true
} );

myObject.a = 3;

myObject.a; // 2
*****
"use strict";

var myObject = {};

Object.defineProperty( myObject, "a", {
    value: 2,
    writable: false, // Brak moliwoci zmiany wartoci!
    configurable: true,
    enumerable: true
} );

myObject.a = 3; // Bd TypeError.
*****
var myObject = {
    a: 2
};

myObject.a = 3;
myObject.a; // 3

Object.defineProperty( myObject, "a", {
    value: 4,
    writable: true,
    configurable: false, // Brak moliwoci konfiguracji!
    enumerable: true
} );

myObject.a; // 4
myObject.a = 5;
myObject.a; // 5

Object.defineProperty( myObject, "a", {
    value: 6,
    writable: true,
    configurable: true,
    enumerable: true
} ); // Bd TypeError.
*****
var myObject = {
    a: 2
};

myObject.a; // 2
delete myObject.a;
myObject.a; // Warto undefined.

Object.defineProperty( myObject, "a", {
    value: 2,
    writable: true,
    configurable: false,
    enumerable: true
} );

myObject.a; // 2
delete myObject.a;
myObject.a; // 2
*****
myImmutableObject.foo; // [1,2,3]
myImmutableObject.foo.push( 4 );
myImmutableObject.foo; // [1,2,3,4]
*****
var myObject = {};

Object.defineProperty( myObject, "FAVORITE_NUMBER", {
    value: 42,
    writable: false,
    configurable: false
} );
*****
var myObject = {
    a: 2
};

Object.preventExtensions( myObject );

myObject.b = 3;
myObject.b; // Warto undefined.
*****
var myObject = {
    a: 2
};

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

myObject.b; // Warto undefined.
*****
var myObject = {
    a: undefined
};

myObject.a; // Warto undefined.

myObject.b; // Warto undefined.
*****
var myObject = {
    // Zdefiniowanie gettera dla 'a'.
    get a() {
        return 2;
    }
};

Object.defineProperty(
    myObject,   // Element docelowy.
    "b",        // Nazwa waciwoci.
    {           // Deskryptor.
        // Zdefiniowanie gettera dla 'b'.
        get: function(){ return this.a * 2 },
        // Upewniamy si, e 'b' bdzie waciwoci obiektu. 
        enumerable: true
    }
);

myObject.a; // 2
myObject.b; // 4
*****
var myObject = {
    // Zdefiniowanie gettera dla 'a'.
    get a() {
        return 2;
    }
};

myObject.a = 3;

myObject.a; // 2
*****
var myObject = {
    // Zdefiniowanie gettera dla 'a'.
    get a() {
        return this._a_;
    },

    // Zdefiniowanie gettera dla 'a'.
    set a(val) {
        this._a_ = val * 2;
    }
};

myObject.a = 2;

myObject.a; // 4

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

("a" in myObject); // Prawda.
("b" in myObject); // Fasz.

myObject.hasOwnProperty( "a" ); // Prawda.
myObject.hasOwnProperty( "b" ); // Fasz.
*****
var myObject = { };

Object.defineProperty(
    myObject,
    "a",
    // Waciwo 'a' bdzie uwzgldniona w typach wyliczeniowych, jak zwykle.
    { enumerable: true, value: 2 }
);

Object.defineProperty(
    myObject,
    "b",
    // Waciwo 'b' nie bdzie uwzgldniona w typach wyliczeniowych.
    { enumerable: false, value: 3 }
);

myObject.b; // 3
("b" in myObject); // Prawda.
myObject.hasOwnProperty( "b" ); // Prawda.

// ......

for (var k in myObject) {
    console.log( k, myObject[k] );
}
// "a" 2
*****
var myObject = { };

Object.defineProperty(
    myObject,
    "a",
    // Waciwo 'a' bdzie uwzgldniona w typach wyliczeniowych, jak zwykle.
    { enumerable: true, value: 2 }
);

Object.defineProperty(
    myObject,
    "b",
    // Waciwo 'b' nie bdzie uwzgldniona w typach wyliczeniowych.
    { enumerable: false, value: 3 }
);

myObject.propertyIsEnumerable( "a" ); // Prawda.
myObject.propertyIsEnumerable( "b" ); // Fasz.

Object.keys( myObject ); // ["a"]
Object.getOwnPropertyNames( myObject ); // ["a", "b"]
*****
var myArray = [1, 2, 3];

for (var i = 0; i < myArray.length; i++) {
    console.log( myArray[i] );
}
// 1 2 3
*****
var myArray = [ 1, 2, 3 ];

for (var v of myArray) {
    console.log( v );
}
// 1
// 2
// 3
*****
var myArray = [ 1, 2, 3 ];
var it = myArray[Symbol.iterator]();

it.next(); // { value:1, done:false }
it.next(); // { value:2, done:false }
it.next(); // { value:3, done:false }
it.next(); // { done:true }

*****
var myObject = {
    a: 2,
    b: 3
};

Object.defineProperty( myObject, Symbol.iterator, {
    enumerable: false,
    writable: false,
    configurable: true,
    value: function() {
        var o = this;
        var idx = 0;
        var ks = Object.keys( o );
        return {
            next: function() {
                return {
                    value: o[ks[idx++]],
                    done: (idx > ks.length)
                };
            }
        };
    }
} );

// Rczna iteracja przez 'myObject'.
var it = myObject[Symbol.iterator]();
it.next(); // { value:2, done:false }
it.next(); // { value:3, done:false }
it.next(); // { value:undefined, done:true }

// Iteracja przez 'myObject' za pomoc ptli 'for-of'.
for (var v of myObject) {
    console.log( v );
}
// 2
// 3
*****
var randoms = {
    [Symbol.iterator]: function() {
        return {
            next: function() {
                return { value: Math.random() };
            }
        };
    }
};

var randoms_pool = [];
for (var n of randoms) {
    randoms_pool.push( n );

    // Nie kontynuuj dziaania bez powizania z ptl for-of!
    if (randoms_pool.length === 100) break;
}
*****

??





52	(	Rozdzia 3. Bd! W dokumencie nie ma tekstu o podanym stylu.

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



					51

