Object

10 0 0
                                        

Object is a collection of data. Data can be a property or a function. These datum are created within the braces {}.

Property is a key and value pair. It is like a variable declaration except you remove the let keyword and you treat the key as a string type name and the value as whatever that name contains.

syntax:

let name = {

    key1  :  value1,

    key2  :  value2,

    "your key3" :  value3,

};

The key is the name in string. If it contains a space, you need to enclose it in quotes. The comma in the last property is optional. Also the above syntax is for creating object literally but if you want to create using the new keyword, this is also possible.

syntax:

let name = new Object();

This syntax creates an empty object. This is equivalent to let name = {} . You see, there is no property inside the braces since it is created as empty. You can add a property by invoking the object name followed by the dot operator or a bracket notation if there is a space in the key name.

syntax:

name.key4 = value4;

name["your key5"] = value5;

You can delete a property by using the delete keyword.

syntax:

delete name.key1;

delete name["your key3"];


==================

let book = {

    "prop 1" : "Reincarnated as a Slime",

    "prop 2" : "Don't know",

    "prop 3" : 2021

}

fun getBook(name){

    name["prop 4"] = "Rimuru Tempest";

    delete name["prop 2"];

    let x = 1;

    while(x <= 5){

        let key = 'prop ${x}';

        document.write(name[key]);

        x++;

    }

}

==================

HTML

getBook(book)

Result:

Reincarnated as a Slime undefined 2021 Rimuru Tempest undefined

Note: 

1. The first undefined is the prop 2 that was deleted. The second undefined is because there are only 4 properties but we ask for 5. So, the fifth is undefined.

2. Assigning the key to a variable and then used it in a dot notation is is not allowed. When you assigned the key to a variable, you can only used the square bracket notation. 

3. The same thing happens when you use computed properties. Computed properties means you assign a temporary variable in one of the properties whose key name depends on the prompt for example. You need to enclose the variable in a square bracket.

================

let x = prompt("Enter key name: ", "prop");

let book = {

    [x] : "Rimuru Tempest"

};

================

Note: If you write in a prompt "author", 

[x] : "Rimuru Tempest" becomes author : "Rimuru Tempest"


using length sample

===================

let book = {

    "prop 1" : "Naruto",

    "prop 2" : "Check Google",

    "prop 3" : "Uzumaki Naruto"

};

let length = (name) => {

    let counter = 0;

    while(true){

        counter++;

        let key = 'prop ${counter}';

        if(name[key] === undefined){

            break;

        }

   }

    return counter - 1;

};

function simpleDisplayOfObjects(name){

    let x = 1;

    let size = length(name);

    while(x <= size){

        let key = 'prop x';

        document.writeln(name[key]);

        x++;

    }

}

===================

HTML

simpleDisplayOfObjects(book)

Result:

Naruto Check Google Uzumaki Naruto

Javascript ProgrammingWhere stories live. Discover now