Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferrenceError while trying to access variable inside of Function constructor

Reading the mdn documentation on the difference between Function constructor and function declaration. The example specified there works on the browser and also on the node.js repl, but on trying it through a file, the node.js process crashed with this error

ReferenceError: x is not defined

This is the program

var x = "bar";

function test() {
    var x = "baz";
    return new Function("return x;");
}

var t = test();
console.log(t());

What might be the possible reason for this example not work as expected when been executed from a file with node.js?

like image 647
0.sh Avatar asked Aug 30 '20 22:08

0.sh


People also ask

How do I fix reference error in JavaScript?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.

Why is my variable not defined JavaScript?

This JavaScript exception variable is not defined occurs if there is a non-existent variable that is referenced somewhere. Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared, or make sure the variable is available in the current script or scope.

What is a ReferenceError?

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .

How do you handle variables not defined?

If you want to check if a variable exists, that can only be done with try / catch , since typeof will treat an undeclared variable and a variable declared with the value of undefined as equivalent.

How to access variables in a constructor function with JavaScript?

Accessing variables in a constructor function using a prototype method with JavaScript? For this, use a “prototype”. JavaScript objects inherit properties and methods from a prototype. For accessing variables, we have also used the “this” in JavaScript.

How to access members of a class by their function object?

This is done similar to how we access members of a class by their object using the “.” operator. The variable can be assigned to the function object inside the function body. So the variable exists only after the function has been called. Once the function has been called, the variable will be associated with the function object.

How to assign a variable outside of a constructor?

If you want to use the variable 'a' outside the constructor, you should declare it outside the constructor but within the scope of the class. It is suggested to declare this variable as a private member so that it cannot be assigned outside the class directly. Show activity on this post.

What is a constructor function in JavaScript?

In this tutorial, you will learn about JavaScript constructor function with the help of examples. In JavaScript, a constructor function is used to create objects. For example, In the above example, function Person () is an object constructor function. To create an object from a constructor function, we use the new keyword.


1 Answers

In the Node REPL, the lexical location of where you're typing in code is the top level, equivalent to typing stuff into the top of a <script> tag in the browser.

Variables defined with var on the top level get assigned to the global object. So, in both Node's REPL and the browser, your

var x = "bar";

results in x being assigned to the global object.

But, in contrast, when you run the code from a file, eg node bar.js, the code that is run is inside a module - it's not on the top level, so variables declared at the top level of such a script do not get assigned to the global object.

The function that is created is global, at the top level, so it can only lexically "see" variables defined at the top level. So, when running the code as a file in Node, since the scope of the code being run is not the top level, the created function can't see x anywhere, so a ReferenceError is the result.

like image 133
CertainPerformance Avatar answered Oct 25 '22 13:10

CertainPerformance