How to global variable access in function using Javascript?

hari prasanth

I have a doubt when I tried to access global variable in two functions. One function is an arrow function another function is a normal function. The arrow function is working fine, but the normal function cannot a print global variable. Why?

Example Code

class Data1{

    constructor(m1){
        this.m1 = m1
    }
}

class Data2 extends Data1{

    func1(){
        console.log(this.m1)
        this.m1 = 20
    }
    func2=()=>{
        console.log(this.m1)
        this.m1 = 40
    }


}

d1 = new Data1(10)
d2 = new Data2()
d2.func1()
d2.func2()

Output

undefined  
20
  • What is difference between normal scope function and arrow scope function?
  • How to access global function inside the normal function?
Rohan Veer

In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever.

With arrow functions the this keyword always represents the object that defined the arrow function.

If you write a constructor in Data1 and bind func1 to the Data1 object it will work. For eg.

constructor(){
    this.func1 = this.func1.bind(this);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i access global variable using function in c++?

How to access a global variable in a different function

How to access a global variable in a lambda function?

Changing global variable using function with parameters Javascript

I need to access variable inside a function using a global variable (Flutter)

Access a function variable outside the function without using "global"

Access a function variable outside the function without using "global"

How to access the function property of a javascript object using variable

how to use a global variable inside a function in javascript?

How to access global variable from a function when the name is same as argument name in JavaScript?

How to change a global variable in a function using parameters

How to access a variable which is inside of a function without defining the variable as global

Using global variable in function

Set global variable from within function using function parameter with JavaScript

function call inside another function with using javascript Global variable

Access global variable within function

Access a global variable in a PHP function

Unable to access global variable in javascript?

How to access global variable from outside of canvas.onmousemove function?

How to access to the global variable from an anonymous function in the Go?

How to access global variable from inside class's function

How to change a global variable within a function using click eventListener function?

How to access a variable passed in through JavaScript function

how to access variable from .getCurrentPosition() function javascript

How to access the variable permalink in this JavaScript function?

How to access javascript function variable in UIWebview

Javascript: How to access local variable in callback function?

How to access the local variable outside the JavaScript function?

Define global variable in a JavaScript function