I am learning some jQuery basics with this great book: http://jqfundamentals.com/book/
There is an example that doesn't return the right result.
Here is the code:
var myName = 'the global object',
sayHello = function () {
console.log('Hi! My name is ' + this.myName);
},
myObject = {
myName : 'Rebecca'
};
var myObjectHello = sayHello.bind(myObject);
sayHello(); // logs 'Hi! My name is the global object'
myObjectHello(); // logs 'Hi! My name is Rebecca'
The log returns undefined instead of the global object for sayHello(); and I'd like to know why...
-
Either this is an implementation issue ('cause this works fine in Chrome)cwallenpoole– cwallenpoole2011年06月21日 19:55:39 +00:00Commented Jun 21, 2011 at 19:55
-
@cwallenpoole . . . either it's an implementation issue, or what?Levi Morrison– Levi Morrison2011年06月21日 20:01:16 +00:00Commented Jun 21, 2011 at 20:01
-
@Levi apparently the "or" is "or I have ADD"cwallenpoole– cwallenpoole2011年06月21日 20:10:44 +00:00Commented Jun 21, 2011 at 20:10
4 Answers 4
It should work properly, unless you've wrapped that code in another function, creating a new variable scope.
Example: http://jsfiddle.net/RKYNn/
As such, myName would not be available as a property on the global object.
So if you did this:
(function() {
var myName = 'the global object',
sayHello = function () {
console.log('Hi! My name is ' + this.myName);
},
myObject = {
myName : 'Rebecca'
};
var myObjectHello = sayHello.bind(myObject);
sayHello(); // logs 'Hi! My name is the global object'
myObjectHello(); // logs 'Hi! My name is Rebecca'
})();
...you'd get undefined because myName is no longer global, and this in the function is a reference to the global object.
4 Comments
$(document).ready()$(document).ready(). It makes sense to me nowMy guess is that you are running onDomReady or onLoad. If I am correct, that is your problem (or appears to be when playing around in jsFiddle). Don't wrap it up and load it in the head or body after your jQuery.
See http://jsfiddle.net/morrison/euprL/
The reason has to do with scoping. What is the scope of variables in JavaScript? sums it up quite well, I think.
Comments
The global object isn't what it's actually going to say. It was implied that it will return the global window object, which is where global variables and such live. Unless you have a global variable named myName the value will be undefined.
Edit
First time through I missed the var myName = 'the global object';. That var makes it it part of the scope it's currently in, so as others have pointed out, unless you're in the global scope, it won't find the variable.
Comments
Tested your code like this and it works:
<html>
<head>
<script type="text/javascript">
var myName = 'the global object',
sayHello = function () {
console.log('Hi! My name is ' + this.myName);
},
myObject = {
myName : 'Rebecca'
};
var myObjectHello = sayHello.bind(myObject);
sayHello(); // logs 'Hi! My name is the global object'
myObjectHello(); // logs 'Hi! My name is Rebecca'
</script>
</head>
</html>
Can you give us some details on the context of this script?
1 Comment
$(document).ready()