Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"use strict" causes undefined error

I am defining the following function in my JavaScript:

function _snr(id) {
    "use strict";
    this.e = "something";
}

I ran my code through JSLint and it suggested that I add "use strict" to the function.

When I do e now throws and undefined error. From some initial investigation it looks as though this which used to refer to _snr is no longer defined.

I have read about "use strict" and discovered that it is used to prevent unsafe practices. Could some one explain what was unsafe about this? What "use strict" is actually doing and how I can fix my code?

like image 854
ferics2 Avatar asked Nov 07 '12 06:11

ferics2


People also ask

Why in strict mode this is undefined?

In strict mode, it is now undefined . When a function was called with call or apply , if the value was a primitive value, this one was boxed into an object (or the global object for undefined and null ). In strict mode, the value is passed directly without conversion or replacement.

What are the disadvantages of using use strict?

If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.

What if you use use strict but don't declare a variable?

In strict mode all variables have to be declared: if you assign a value to an identifier that has not been declared as variable, function, function parameter, catch-clause parameter or property of the global Object , then you will get a ReferenceError .

What is the use of use strict?

The "use strict" Directive The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive.

What does'use strict'mean in JavaScript?

The "use strict" directive is not a statement, but a literal expression, which was ignored by many earlier versions of JavaScript. The main aim of "use strict" is to indicate that the code should be executed in "strict mode".

What does ‘use strict’ not allowed in function mean?

This JavaScript exception ‘use strict’ not allowed in function occurs if strict modes ‘use strict’ statement is used at the beginning of a function with default parameters, rest parameters, or destructuring parameters.

Why am I getting error code const under strict mode?

Usually this error occurs when the version of node against which the code is being executed is older than expected. (i.e. 0.12 or older). if you are using nvm than please ensure that you have the right version of node being used. You can check the compatibility on node.green for const under strict mode

What does use strict mean in ECMAScript?

The "use strict" Directive The "use strict" directive was new in ECMAScript version 5. It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode".


1 Answers

If a function is called without setting its this, in non–strict mode its this will be set to reference the global (window in a browser) object. In strict mode, it wil remain undefined.

If your function is called as _snr(...) then its this is not set, so in non–strict mode this will be set to the global object so this.e = ... references (or creates due to the assignment) a global e property.

However, in strict mode this will be undefined, and trying to access a property of undefined throws an error.

It's explained in ECMA-262 §10.4.3 Entering Function Code.

Edit

If you wish to access the global object from inside a function in a manner that is consistent with both strict and non–strict mode, you can use something like:

var _snr = (function(global) {
    return function (id) {
        global.e = "something";
    };
}(this));

In non-strict mode, you can do:

function _snr(id) {
    var global = (function(){return this;}());
    global.e = "something";
}

so that global within the function references the global object and you don't have to worry about how the function is called. But the second example won't work in strict mode.

Other answers:

I have read about "use strict" and discovered that it is used to prevent unsafe practices. Could some one explain what was unsafe about this?

Absolutely nothing in this particular case.

However, in a more general case, it was considered a good idea to be able to execute code within a context that stopped it accessing the global object directly. The second example above shows how that can be done in non–strict code (i.e. how you can get direct access to the global object from inside a function context).

What "use strict" is actually doing

It is stopping this being set to the global object if the call sets it to undefined or null. See above for the consequence of that.

and how I can fix my code?

See above.

Oh, and finally, there is an informative summary of strict mode in ECMA-262 Annex C The Strict Mode of ECMAScript.

like image 200
RobG Avatar answered Oct 03 '22 04:10

RobG