JavaScript : OOPS



<---- OOPS IN JAVASCRIPT ---->

What is object literals ?
What is "this" object ?

What is object Literal?
Object literal is simply a key:value pair data structure.

storing variables and functions together in one container,
we can refer this as an objects.

Object = school bag.

How to create an object?

1st way :-

let bioData  = {
    myName : "Himanshu Kumar Tiwari",
    myAge : 19,
    myAddress : "Shahpur Daltonganj",
    getData : function () {
        console.log(" My Name is " + bioData.myName + "and my age is " + bioData.myAge);
    }
}

console.log(bioData.getData());

2nd way :-
no need to write function keyword 

let bioData  = {
    myName : "Himanshu Kumar Tiwari",
    myAge : 19,
    myAddress : "Shahpur Daltonganj",
    getData () {
        console.log(" My Name is " + bioData.myName + "and my age is " + bioData.myAge);
    }
}

bioData.getData();


what if we want object as a value inside an Object.

let bioData  = {

    myName : {
        firstName : "Himanshu",
        lastName : "Tiwari",
        myCollege : {
            college : "GLA COLLEGE",
        },
    },
    myAge : 19,
    myAddress : "Shahpur Daltonganj",
    getData () {
        console.log(" My Name is " + bioData.myName + "and my age is " + bioData.myAge);
    }
}

console.log(bioData.myName.myCollege.college )


What is this Object?

The defination of 'this' object is that it contain the current context.
The this object can have different value depending on where it is placed.

for example 1
console.log(this.alert("Namaste World"));
it returns to the current vontext and that is window global object.

example 2

function myName(){
    console.log(this);
}

myName();

example 3 

let myNames = "Himanshu"
function myName(){
    console.log(this.myName);
}

myName();

example 4

const obj = {
    myAge : 19,
    myName(){
        console.log(this.myAge);
    }
}
obj.myName();

❗❗❗ this object not work with Fat arrow function ❗❗❗