Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript this Inside Inner Function

const person = {
    name : 'Jack',
    age: 25,
    // this inside method
    // this refers to the object itself
    greet() {
        console.log(this);        // {name: "Jack", age ...}
        console.log(this.age);  // 25
        // inner function
        function innerFunc() {
                    // this refers to the global object
            console.log(this);       // Window { ... }
            console.log(this.age);    // undefined
            
        }

        innerFunc();
    }
}

person.greet();
 
PREVIOUS NEXT
Tagged: #javascript #Inside #Inner #Function
ADD COMMENT
Topic
Name
1+1 =