kristian@ktae.co.uk Posted July 10, 2020 Share Posted July 10, 2020 I don't seam to be able to find the "with" keyword explained in either the JS tutoral or the JS reference. Please, would someone share a link to the explaination? Link to comment Share on other sites More sharing options...
Ingolme Posted July 10, 2020 Share Posted July 10, 2020 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 More sharing options...
kristian@ktae.co.uk Posted July 11, 2020 Author Share Posted July 11, 2020 (edited) 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 July 11, 2020 by kristian@ktae.co.uk Link to comment Share on other sites More sharing options...
Ingolme Posted July 11, 2020 Share Posted July 11, 2020 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now