2

In a javascript file, when I declare a function using function keyword, I can placed my function after my caller function, something like

// test.js
function myCaller() {
 foo('hello world'); // this works!
 this.foo('hello world'); // this works!
}
function foo(text) {
 console.log('foo called!', text);
}
myCaller()

but if I turned the foo into an arrow function, and placed it at same position, then in myCaller function, it said foo is not defined, also it won't work if I use this keyword to locate the foo function, which I assume the this refers to global/document level

// test.js
function myCaller() {
 foo('hello world'); // es-lint warning: 'foo' was used before it was defined
 this.foo('hello world'); // compilation error: foo is not defined
}
const foo = (text) => {
 console.log('foo called!', text);
}
myCaller();

- I was wrong, I thought the 2nd approach without using this does not work because of javascript compilation error of not defined, but it was actully my eslint error - 'foo' was used before it was defined, does it mean it's not recommend to do this way?

Why is that and does it mean we have to declare the arrow function always above caller function? Is there any alternative way to solve that?

const foo = (text) => {
 console.log('foo called!', text);
}
// test.js
function myCaller() {
 foo('hello world'); // this works! no es-lint warning now
 this.foo('hello world'); // no compilation error but there is a run-time error : 'this.foo is not a function'
}
myCaller();

In addition, I found when declare the arrow function inside a javascript class, it would work with this keyword only with caller function being arrow function as well, if caller function is with function keyword, this will not work...

// myTest.js
class myTest {
 myCaller() {
 foo('hello world'); // compilation error: foo is undefined
 }
 myCaller2 = () => {
 this.foo('hello world'); //this works!
 }
 foo = (text) => {
 console.log('foo called!', text);
 }
}
new myTest().myCaller();
new myTest().myCaller2();

VLAZ
29.6k9 gold badges65 silver badges88 bronze badges
asked Aug 23, 2020 at 5:46
12
  • 2
    nothing to do with arrow, has to do with scope of const Commented Aug 23, 2020 at 5:49
  • stackoverflow.com/questions/54933689/… Commented Aug 23, 2020 at 5:51
  • 3
    also, this.foo would subsequently fail even if const foo was above myCaller - since const and let variables are not added to the "global" object - this would likely be the global object (window on a browser) in your code Commented Aug 23, 2020 at 5:52
  • @JaromandaX should I remove the const? I cannot remove the const...if I remove the const, there is compile error said foo is not defined Commented Aug 23, 2020 at 5:55
  • @BaruchMashasha thanks for the link, but I don't think that helps here, as I am not executing myCaller function, as it's already generated compilation error, in addition, why it works when it uses function keyword but just not for arrow function? Commented Aug 23, 2020 at 6:02

1 Answer 1

2

Any variables (except let and const variables) that you declare or functions that you define in global context (say, directly in test.js) will be attached to Window object. So, when you write,

// test.js
function myCaller() {
 foo('hello world'); // this works!
 this.foo('hello world'); // this works!
}
function foo(text) {
 console.log('foo called!', text);
}
myCaller()

Both myCaller and foo are attached to window object as properties. Now you can refer to them either directly like foo()(this is implicit here) or this.foo() or even window.foo(). Since js uses hoisting, these variables or functions are first attached to the context and then starts executing.

But const and let are not attached to the Window object (otherwise they would be accessible everywhere, behaving exactly like var). So when you write

// test.js
function myCaller() {
 foo('hello world');
 this.foo('hello world');
}
const foo = (text) => {
 console.log('foo called!', text);
}
myCaller();

Javascript engine scans through the entire script to see if there are any variables or functions defined. In this hoisting phase, if it finds a const or let, it allocates memory for them but won't make them part of Window object. So, when you are referring to this in global context, it is referring to window object and foo is not a property of window object. That is why, even if you put const foo above myCaller definition, it won't work.

In case of class, if you try to call foo() directly without referring to this, it tries to access it from the enclosing contexts, where it is not defined in your example. So it throws error. If you define another foo() outside the class with var or directly as foo = (text) => ..., it will work.

(The this may not be always referring to global context. Functions and classes can have their own custom context, and this would refer to that context. But when no custom context is defined by the functions or classes, the global context will be the one the this keyword would be referring to. The this is undefined for functions and classes by default in strict mode. There are several such caveats to be taken into consideration.)

answered Aug 23, 2020 at 7:45
Sign up to request clarification or add additional context in comments.

5 Comments

You should explain why this inside myCaller is (or might not) be the global object...
foo() inside the class isn’t necessarily looked up from a global context, just from any surrounding scope.
JavaScript is also compiled, just on an as-needed basis. It can still throw compilation errors; syntax errors are thrown in the compilation/parsing phase. That’s distinct from a runtime error.
Thank you @chethan7! It explained very well! One thing I would like to understand betteris, in case of class, if I declare foo() outside my class, then I can call foo() directly inside my class method, does it mean that now it's because the foo() is attached to the global windows context and that's why I can call it inside my class method?
And you mentioned That is why, even if you put const foo above myCaller definition, it won't work. Actually if I directly call the foo() without this, it did work...It's just not work when I call this.foo(), but since you mentioned, when met with const or let, JS allocates memory for them but not make them part of Window object, that'd prob explain why this.foo() not work because currently the this refers to windows context, hwever, why calling without this would work this time?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.