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?
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.
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.
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() .
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With