Skip to content

Instantly share code, notes, and snippets.

@vernonk
Last active December 15, 2015 04:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernonk/5204305 to your computer and use it in GitHub Desktop.
Save vernonk/5204305 to your computer and use it in GitHub Desktop.
Inspired by @Jack_Franklin here's another little JS quiz.
function getMe() {
console.log(this); // what is `this`?
}
var superman = {
fname: "Clark",
lname: "Kent"
};
getMe.call(superman); // what is `this` in getMe?
var myObj = {
sayWhat: function() {
console.log(this); // what is `this`?
}
};
$('.el').each(function() {
console.log(this); // what is `this`?
});
@benhowdle89
Copy link

function getMe() {
console.log(this); // what is this? -> window
}

var superman = {
fname: "Clark",
lname: "Kent"
};

getMe.call(superman); // what is this in getMe? -> superman

var myObj = {
sayWhat: function() {
console.log(this); // what is this? -> myObj
}
};

$('.el').each(function() {
console.log(this); // what is this? -> current iteration of $('.el')
});

@vernonk
Copy link
Author

vernonk commented Mar 20, 2013

It's probably just semantics (i.e. the way I'm reading it) but the jQuery iterator isn't a reference to the current jQuery Object of the .el but a reference to the DOM node itself. Unlike a lot of other jQuery methods, this actually has to be wrapped in $() to get a jQuery Object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment