Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to enable "strict mode" in FireBug and Chrome's console?

Tags:

With this page:

<!DOCTYPE html> <html>   <head>     <script>         "use strict";         var foo = 2;         delete foo;     </script>   </head>   <body></body> </html> 

Firebug console gives:

applying the 'delete' operator to an unqualified name is deprecated >>> foo ReferenceError: foo is not defined foo 

But then this is successful:

>>> var bar = 2; undefined >>> delete bar; true 

Even if you comment out delete foo; so that the script does not break, deleting bar is still successful despite the fact it "is a property of a Global object as it is created via variable declaration and so has DontDelete attribute":

>>> foo 2 >>> delete foo false >>> var bar = 2; undefined >>> delete bar true 

Is it possible to enable "strict mode" in FireBug and or Chrome's console?

like image 959
AJP Avatar asked Jul 26 '12 20:07

AJP


People also ask

How do I use strict on chrome console?

Actually you can just write 'use strict' before your code without the need to wrap it within an IIFE. @Shayan Honestly I am not sure whether this example in particular should return undefined . Try this 'use strict'; (function() { console. log(this)} )() .

What is use strict JS?

The "use strict" Directive 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". With strict mode, you can not, for example, use undeclared variables.


1 Answers

The firebug console works by wrapping all the code in an "eval" call so the first statement in your script is no longer "use strict" - hence it is disabled. You could try wrapping your code in a function to enforce "use strict" for that particular function but the best solution I know of is to skip the console and test straight in the page itself.

like image 62
zoranc Avatar answered Oct 05 '22 23:10

zoranc