473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

JScript scope: "myFunction" in window ?

I'm trying to learn something about JScript engine.

I have the following example:

<script>(function() {

poop = "brown";

myFunction = function myFunction() {}

}) ();

alert( poop ); // "brown" in IE
alert( "myFunction" in window ); // false in IE.
alert( typeof myFunction ); // false in IE.
function asdf() {
alert( 'ddd' + myFunction); // doesn't run in IE.
}

</script>
<body onload='asdf()'>fdf</body>
<script>asdf(); // doesn't run in IE. </script>

but if I define:

window.myFunction = function myFunction() {}

I get: true for in operator, and "function" for typeof op.

poop variable gets resolved but myFunction doesn't unless you tack it
explicitly onto window.

What is IE doing here with the scope of function references? Where is
myFunction?

Garrett
--
Programming is a collaborative art.

Aug 31 '07 #1
2 1914
On Aug 31, 3:57 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
I'm trying to learn something about JScript engine.

I have the following example:

<script>(function() {

poop = "brown";

myFunction = function myFunction() {}

}) ();

alert( poop ); // "brown" in IE
alert( "myFunction" in window ); // false in IE.
alert( typeof myFunction ); // false in IE.

function asdf() {
alert( 'ddd' + myFunction); // doesn't run in IE.

}

</script>
<body onload='asdf()'>fdf</body>
<script>asdf(); // doesn't run in IE. </script>

but if I define:

window.myFunction = function myFunction() {}

I get: true for in operator, and "function" for typeof op.

poop variable gets resolved but myFunction doesn't unless you tack it
explicitly onto window.

What is IE doing here with the scope of function references? Where is
myFunction?

Garrett
--
Programming is a collaborative art.
My first guess is:

(function() {
myFunction = function myFunction() {};
})();

Here, although you'd think myFunction would first be declared as a
global variable (aka property of the "window" object), the right side
of the assignment operation actually evaluates first, so the function
"myFunction" gets declared within the scope of you closure and its
reference gets assigned to the name "myFunction", which by then is
defined in the closure's scope, so it never goes down the scope chain
to the "window" object. Assigning it as a property of the window
object in the first place will actually create a property on the
"window" object, so this behavior is as expected. (As strange as it
may seem.)

-David

Aug 31 '07 #2
On Aug 31, 4:19 pm, David Golightly <davig...@gmail.comwrote:
On Aug 31, 3:57 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
I'm trying to learn something about JScript engine.
I have the following example:
<script>(function() {
poop = "brown";
myFunction = function myFunction() {}
}) ();
alert( poop ); // "brown" in IE
alert( "myFunction" in window ); // false in IE.
alert( typeof myFunction ); // false in IE.
function asdf() {
alert( 'ddd' + myFunction); // doesn't run in IE.
}
</script>
<body onload='asdf()'>fdf</body>
<script>asdf(); // doesn't run in IE. </script>
but if I define:
window.myFunction = function myFunction() {}
I get: true for in operator, and "function" for typeof op.
poop variable gets resolved but myFunction doesn't unless you tack it
explicitly onto window.
What is IE doing here with the scope of function references? Where is
myFunction?
Garrett
--
Programming is a collaborative art.

My first guess is:

(function() {
myFunction = function myFunction() {};

})();

Here, although you'd think myFunction would first be declared as a
global variable (aka property of the "window" object), the right side
of the assignment operation actually evaluates first, so the function
"myFunction" gets declared within the scope of you closure and its
reference gets assigned to the name "myFunction", which by then is
defined in the closure's scope, so it never goes down the scope chain
to the "window" object. Assigning it as a property of the window
object in the first place will actually create a property on the
"window" object, so this behavior is as expected. (As strange as it
may seem.)

-David
I came to that assumption this morning shortly after I woke up.

It makes sense. It's an adverse affect to IE's interpreting a
FunctionDeclaration when it sees an Identifier in a
FunctionExpression,

You explained it very well, so I can't comment on it much more.

Garrett

Sep 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Bokma | last post by:
I have two windows in a frame. I want to be able that each can open a pop up window and that the handle to that window can be stored somewhere, so that each can talk to the pop up. is it...
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
2
by: cmay | last post by:
I am having a problem with the use of "this". Maybe someone can help me out here, let me know if I am doing this wrong: Lets say I have an object MyObject, which has local variables, and some...
1
by: lwhitb1 | last post by:
I have been trying to load a javascript function from the body onload html tag, but I only want the function to load the first time the page is loaded: I have investigated but haven't found...
4
by: jdc_1040 | last post by:
Could someone explain what the scope chain looks like to code running inside eval? (Please no "eval is the devil" essays :) ) Let's say I have a string of JavaScript that looks like this: var...
10
by: Gernot Frisch | last post by:
Hi, I'm currently writing: <span onclick="window.open(...);">Klick Here</span> but I want to use the <a href> for this, since it is defined in the css script the way I want my link to open....
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
11
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.