473,663 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JScript scope: "myFunction " in window ?

I'm trying to learn something about JScript engine.

I have the following example:

<script>(functi on() {

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.myFuncti on = 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 1940
On Aug 31, 3:57 pm, "dhtmlkitc...@g mail.com" <dhtmlkitc...@g mail.com>
wrote:
I'm trying to learn something about JScript engine.

I have the following example:

<script>(functi on() {

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.myFuncti on = 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...@g mail.com" <dhtmlkitc...@g mail.com>
wrote:
I'm trying to learn something about JScript engine.
I have the following example:
<script>(functi on() {
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.myFuncti on = 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
FunctionDeclara tion when it sees an Identifier in a
FunctionExpress ion,

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
3541
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 possible that window 1 in a frameset calls a function in window 2, ie. something like: window 1: ....
13
3074
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 across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
2
1639
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 methods. One of the local variables is an HTML input box. On mousedown I want to execute some code like this: function MyObject(inputFieldID){
1
8729
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 anything that works.. I thought about cookies, but what if the user's disable them? My code: //I want to call the Toggle function below only on the 1st time the page is loaded:
4
6574
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 myVar1 = 1; var myVar2 = 3; window.someFunc = function() { myVar1++; alertFunc(); }; function anotherFunc() { myVar2++; alertFunc(); }
10
3731
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. Now, if I use a href, the href will be executed as well. What can I du in order to make the href not execute if javascript is enabled?
37
3926
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 a function
5
6481
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); td.onclick = myfunction; function myfunction(event) { alert(event);
11
2439
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"> ..historylinks {
0
8436
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.