Jump to content
Sign in to follow this  
kristian@ktae.co.uk

Javascript Tutoral and Reference missing "With"

Recommended Posts

The reason most tutorials don't teach it is because it is usually bad practice to use it.

The "with" keyword takes the properties and methods of an object and treats them as variables inside the braces. For example:

var obj = {
  x : 5,
  y : 10
};

with (obj) {
  alert(x + " , " + y);
}

 

Share this post


Link to post
Share on other sites
11 hours ago, Ingolme said:

The reason most tutorials don't teach it is because it is usually bad practice to use it.

The "with" keyword takes the properties and methods of an object and treats them as variables inside the braces. For example:


var obj = {
  x : 5,
  y : 10
};

with (obj) {
  alert(x + " , " + y);
}

 

Thanks, I realise it is bad practice.
However, I have found some articles explaining when it is useful.
So, you are saying it was deliberately excluded.

Edited by kristian@ktae.co.uk

Share this post


Link to post
Share on other sites

That's probably the case. I can't say for sure.

There's nothing with can do that can't be solved by simple dereferencing. I would have to question any source that says there is a good place to use it. The problem is that it can override variables and functions unintentionally. When you're reading code within a with block which was written by somebody else, it's impossible to know whether they intended to use an object property or some external variables.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...