With statement in JavaScript

The with statement extends the scope chain for a statement. (from MDN)

with (expression) {

expression: Adds the given expression to the scope chain used when evaluating the statement. The parentheses around the expression are required.
statement: Any statement. To execute multiple statements, use a block statement ({ … }) to group those statements.

var value = 6;
var test = {

 alert(value); //5
 name = 'added'; //you can assign a new property and only use it inside with statement
 with(inner){//inner with test
 alert(value); //9

alert(value);//6, unaffected global value
alert(test.name);//this will be undefined

The with statement is consider harmful as shown in YUI.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s