Thank you to everyone for the effort put into this thread. I have
reservations about responding at all after reading the words in some of
the posts, but it seems to me more important to provide some kind of
closure.
I was wrong in my observation in the initial post. I read this thread
- twice - and built my own test script (included below) both to confirm
the thread and as pennance for beginning the thread with an erronious
observation. On digging deeper, I found that the properties which I
thought were being created were in fact declared elsewhere.
JavaScript does not read or write to with(object) properties which do
not exist.
It is riskier to use with () (as opposed to an explicit reference) as
the scope chain will be explored if a property is not found.
The only point I disagree with is the point of with(obj) not resulting
in shorter code. Perhaps not for a one-line situation, but when
several lines of code are involved, with(obj) can not only reduce the
coding, but can make the code easier to read.
Here's my test script. The results change slightly and in an expected
manner if the function reference is preceeded with "new".
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
var a
var myObj = {
b: undefined,
myFunc: function(c) {
var d
with (this) {
a = "Mares eat oats."; // Should assign to Global variable a
b = "Does eat oats."; // Should assign to myObj.b (same as this.b)
unless "new function" is used
c = "Little lambs eat ivy."; // Should assign to parameter c
d = "A kidd'l lee divey two."; // Should assign to local variable d
e = "Wouldn't you?"; // Should create and assign to global variable
e
};
alert('this.a: ' + this.a); // undefined
alert('this.b: ' + this.b); // Does eat oats (undefined if new
function())
alert('this.c: ' + this.c); // undefined
alert('this.d: ' + this.d); // undefined
alert('this.e: ' + this.e); // undefined
return;
}
}
// Do it
myObj.myFunc('howdy dowdy');
alert('global.a: ' + (typeof a == "undefined" ? "undefined" : a)); //
Mares eat oats
alert('global.b: ' + (typeof b == "undefined" ? "undefined" : b)); //
undefined
alert('global.c: ' + (typeof c == "undefined" ? "undefined" : c)); //
undefined
alert('global.d: ' + (typeof d == "undefined" ? "undefined" : d)); //
undefined
alert('global.e: ' + (typeof e == "undefined" ? "undefined" : e)); //
Wouldn't you?
</script>
</body>
</html>