Jump to content

Javascript Tutoral and Reference missing "With"


kristian@ktae.co.uk

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);
}

 

Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...