Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'strict' mode in Chrome 'JavaScript Console' [duplicate]

I am practicing JavaScript on Chrome's 'JavaScript Console' ( version: 35.0) and I am unable to use the 'use strict' clause as expected.

For the following code snippet :

var obj={x:1,y:2}

//Define new property with 'writable' flag as false.
Object.defineProperty(obj, "z", {value:3, writable:false, enumerable:false, configurable:false})

// Try to change the property 'z',
"use strict"; obj["z"]=4

Output: 4

As per my understanding, changing value of a 'non-writable' property will silently fail in non-strict mode and throw 'TypeError' in strict mode, But I don't see the exception.

console.log(obj)

Object {x: 1, y: 2, z: 3}

Even though the property value is not changed but I am expecting a exception. Please correct if I am doing something wrong ?

like image 660
Pradeep Avatar asked Jun 23 '14 15:06

Pradeep


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 the correct way to run a JavaScript in strict mode?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.

Should I use strict mode JavaScript?

First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see . call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode.

How do I write multiple lines of JavaScript in Chrome console?

If you want to write multiple lines of commands in the Chrome console, first open the console (CMD + Shift + J on Mac / CTRL + Shift + J on Windows). Now while inside the Console, write any line of code, and then hold down Shift + Enter.


1 Answers

The easiest way to use strict mode is to use an IIFE (immediately Invoked Function Expression) like so:

(function()
{
    'use strict';
    var foo = 123;//works fine
    bar = 345;//ReferenceError: bar is not defined
}());

To create a new-line in the console, use shift + enter, or write your code in a separate editor first, then copy-paste it to the console. Setting up a fiddle is all fine and dandy, but just test your code with the markup it was written for (ie: just clear the browser cache and test).
However, I'd urge you to install node.js, still. It's a lot easier to test your code, or validate it (both syntactically and coding-style wise) using JSHint. There are also a lot of ways to examine and your code that run out of node.js, so it's a really good development tool to have

like image 187
Elias Van Ootegem Avatar answered Oct 10 '22 04:10

Elias Van Ootegem