Kamarey

Found this strange thing (see comments in code)

Code Snippet

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body onload="OnLoad()">

</body>

</html>

<script type="text/javascript">

function MyClass()

{

}

function OnLoad()

{

alert(length); // length is 'undefined'

// MyClass is just some object to use it with 'with' statement

with (MyClass) // the mystery start here

{

// after the 'with' statement we have a 'length'

// variable from nowhere and it is 0

alert(length);

// even after assigning a different value

length = 123;

// it is still equal to 0

alert(length);

// lets create a new local variable with the same name

// and assign it some value

var length = 456;

// the local 'length' was not created and we still have

// our mystery 'length' that is 0

alert(length);

}

// after the 'with' statement ends, everything start to be ok

alert(length); // length is 'undefined'

var length = 78; // defining local 'length'

alert(length); // length is '78'

}

</script>

Moreover, you can write this:

Code Snippet

// here is a mystery array with strange value within.

// it will work even out of 'with' statement

alert([456654] + ", " + ["jhgjfkg"] + ", " + [-35454]);

Somebody can explain this

P.S. Sorry for some broken text/color formatting: it's a 21 century outside and Microsoft still can't make normal text editorSmile



Re: Internet Explorer Web Development Strange 'length' variable in javascript when using 'with' statement

John Sudds - MSFT

If you take a careful look at what you are passing to the with statement, you will realize you have specified a Function object, not an object instance. Apparently, the length property refers to the arguments array for the function. So, if you write:

function MyClass(arg)

{

}


The mysterious length value is now 1. Since the length of the arguments array is fixed, it will always be 0 even if you attempt to assign a value to it.

If you really intend to pass a class instance to the with statement, you should write:

with (new MyClass) // the mystery start here


With a new class instance, the length variable acts as you expect it to.





Re: Internet Explorer Web Development Strange 'length' variable in javascript when using 'with' statement

Kamarey

Looks like you are right... thank for the answer. What about the second code snippet with the mystery array Where it came from



Re: Internet Explorer Web Development Strange 'length' variable in javascript when using 'with' statement

John Sudds - MSFT

Can you provide the expected output of the alert On both Internet Explorer 7 and Firefox 2.0.0.3, I see:

456654, jhgjfkg, -35454

It doesn't seem very surprising.






Re: Internet Explorer Web Development Strange 'length' variable in javascript when using 'with' statement

Kamarey

You are right, that's the output:

456654, jhgjfkg, -35454

So somewere should be defined an array with such values, doesn't it What is this array is and where and why it's defined