Is the value of a "non-existent variable" the value "undefined" in nonlinear code?

This paragraph is from the book JavaScript: The Definitive Guide, 6th Edition , page 58:

When an identifier appears on its own in the program, JavaScript assumes that it is a variable and looks at its value. If a variable with this name exists, the expression evaluates to a value undefined

. However, in ECMAScript 5 strict mode, attempting to evaluate a non-existent variable is instead referred to a ReferenceError.


First, let me explain how I interpret some of the phrases used in the paragraph.

"... the identifier appears by itself in the program, ...":

I am assuming that the author means the case where the ID is parsed as an expression (in which case ID resolution is performed). For example:

function func ( arg ) {
    var local = helper( arg );
} 

      

Here func

, arg

(which appears twice), local

and helper

are all identifiers, but only helper

and arg

(only in its second appearance!) Are expressions. Thus, only these two identifiers can cause a reference error.

"... a variable with this name exists ..." and "non-existent variable":

I am assuming the author means the id that evaluates the unresolvable link.


Now, correct me if I'm wrong, but ...

When an identifier is evaluated that is parsed as an expression (a PrimaryExpression

to be precise), the identifier is resolved. The result of this process is always a value of type Reference. In other words, the ID will evaluate the link.

The link has a base meaning and a reference name. The specified name is the identifier text, and the base value is the environment record that has a binding for that name (the scope containing such a variable). However, if identifier resolution cannot resolve the reference name, the reference's base value will be the value undefined

.

Thus, a "non-existent variable" therefore evaluates to a reference whose underlying value is undefined

.

Note that evaluating a non-existent variable does not raise a pivot error. The error is called later when the interpreter gets the value of the link (via GetValue()

). This, however, may not always happen - for example, typeof x

it will not retrieve the value of a link if it x

evaluates to an unresolvable link, and thus a link error does not occur.


The paragraph at the beginning of my question states that in nonlinear code, non-existent variables are evaluated to undefined

, whereas when the code is strictly evaluated, such an identifier throws a reference error. I believe both of these statements are wrong - a non-existent variable evaluates to a reference with an underlying value undefined

, and a reference error is not thrown in strict mode.

When the interpreter tries to retrieve the value of such a reference (which may or may not happen, depending on the "external" expression or operator in which the identifier appears), a reference error is generated. This behavior is no different between lax and strict modes.


So, is this paragraph correct? If not, have I identified the error (s) correctly? (Also, if there is a mistake in my text, please correct me.)

+3


source to share


2 answers


Does a "non-existent variable" know the value of "undefined" in nonlinear code?

> foo;
ReferenceError: foo is not defined
> var x = foo;
ReferenceError: foo is not defined

      

This behavior is also true in strict mode:

> (function () { "use strict"; foo; }());
ReferenceError: foo is not defined
> (function () { "use strict"; var x = foo; }());
ReferenceError: foo is not defined

      



The only difference I know about strict mode and non-strict mode in terms of variable resolution is that when assigning to a variable, if the variable is not declared, a ReferenceError is thrown in strict mode, however, in non-string mode, a global variable is implicitly created.

Appendix C (ECMAScript Strict Mode):

Assigning an undeclared identifier or otherwise undecidable reference does not create a property on the global object. When a simple assignment occurs in strict mode code, its LeftHandSide should not evaluate an unresolvable reference. If it raises a ReferenceError (8.7.2) exception.

+2


source


Yes, even if you declared it with

var someVar;

      



it still evaluates undefined.

0


source







All Articles