I create a new global object and then put everything in to that new object so it doesn't interfere with anything else anyone else does. After calling the document.ready() function this gets moved to window.bf for unknown reasons. I've reduced this down to a simple example. Anyone have any ideas on what is happening?
<html>
<head>
<title>Test</title>
<script src=".11.1/jquery.min.js"></script>
<script>
debugger;
var bf = {}; // Namespace
bf.vars = [];
bf.vars['onclick'] = "";
$(document).ready(function() {
bf.noop = function() {}
bf.noop();
window.bf.noop();
});
</script>
</head>
<body>Test</body>
</html>
I create a new global object and then put everything in to that new object so it doesn't interfere with anything else anyone else does. After calling the document.ready() function this gets moved to window.bf for unknown reasons. I've reduced this down to a simple example. Anyone have any ideas on what is happening?
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
debugger;
var bf = {}; // Namespace
bf.vars = [];
bf.vars['onclick'] = "";
$(document).ready(function() {
bf.noop = function() {}
bf.noop();
window.bf.noop();
});
</script>
</head>
<body>Test</body>
</html>
So I wind up with two "bf"s. One is the original global object and the second one is the window.bf object. My problem is: Originally, the global object had all of the functions and variables in it. Now the global variable has nothing except bf.vars (an array). Everything else is now in the window.bf object (including everything that was supposed to go in to the global bf.vars array yet I never asked for that to happen.
Up until last Friday (9/25/2015) this did not happen. Now it does.
Update: I just tried removing the document.ready() function. If you remove the document.ready() part - then the global bf object goes back to being a global variable. So this appears to have something to do with jQuery itself.
Before anyone asks: The reason the document.ready() is being used is because some of the functions I define make reference to locations that need to be loaded and jQuery says to use the document.ready() function in order to ensure all elements have been loaded before execution starts. The bf.noop() function is just to give an example of a function and to show that the function no longer shows up in the global bf object.
Screen capture with document.ready():
Screen capture without document.ready():
Better?
Share edited Sep 29, 2015 at 15:41 Mark Manning asked Sep 29, 2015 at 15:25 Mark ManningMark Manning 1,47713 silver badges14 bronze badges 9-
In a browser, the global context is the
window
object. Every variable you declare withvar
in the global context (that is, outside of any function) will bee a property of thewindow
object. – Pointy Commented Sep 29, 2015 at 15:29 -
... and the
$(document).ready()
thing here has no effect on anything, other than delaying for a little while the creation of the "noop" property on the object referenced bybf
. – Pointy Commented Sep 29, 2015 at 15:31 - If I run the debugger in FireFox. There are two variables. The global bf object (it shows up as just +bf). A second entry shows up after the document.ready() is through (ie: window->+bf). If the document.ready() is left off only the first one (ie: +bf) shows up. WITH: the +bf only as vars and window->bf has vars and functions. WITHOUT: the +bf has both vars and functions. – Mark Manning Commented Sep 29, 2015 at 15:33
-
1
No, that's really not how it works. When you create the global
bf
, that immediately createswindow.bf
because that's what global variables are. – Pointy Commented Sep 29, 2015 at 15:36 - 1 I tried to edit my reply - but too late. The reason for the "no such..." message is because bf.noop() has not yet been instantiated because of the document.ready() function. I believe rossipedia has the right answer. It is a problem of scope. Some of my function calls are outside of the document.ready() and thus are executed first, before everything is ready. – Mark Manning Commented Sep 29, 2015 at 15:57
3 Answers
Reset to default 6In the top-level scope of a script tag, all variables get implicitly attached to window
.
The good news is, you actually don't have two bf
objects, you only have one with two ways to access it.
var bf = {};
bf.foo = "bar";
document.getElementById('output').innerHTML = JSON.stringify(window.bf, undefined, 4);
<div id="output"></div>
A variable declared outside the scope of a function is bound to the global scope. Since you're declaring var bf = {}
inside the script tag, its parent is the window
object.
This MDN document is a good read on variables in JavaScript.
Rossipedia has both two good answers as well as leading me to find the problem. See his posts.
For unknown reasons, jQuery version 1.11.1 (I'm using v1.11.3 and thus the 1.11.1|3 in my post) has some kind of an error that was fixed in jQuery version 2.1.3.
But I also found that one of the programs I use to generate the menu for our site was generating an old function name (showIt instead of show_menu) which was causing problems also. By fixing the generated function name the problem has gone away.
This does NOT mean that the jQuery folks can go "Yeah! We don't have to fix anything!" because this is a valid error and the simple code I posted shows how to break jQuery which might mean there is some way to cause real problems with jQuery. So basically, this should be looked at by someone more in tune with jQuery to find out why the code I posted does what it does. Then they can e back and say "You idiot! You did BLAH! and you shouldn't have!" or maybe "Thanks for locating this really obscure problem that no one else in the entire world has e across or cares about." In any event - this is now a known problem. :-)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744850924a4597119.html
评论列表(0条)