Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

John Dugan
PLUS
John Dugan
Courses Plus Student 13,165 Points

JavaScript Semantics Confusion

Hey Folks,

My new years resolution is to become a js ninja. I recently watched Jim Hoskins' JavaScript Foundations course (which was fantastic). That gave a pretty clear definition of the basic js terms below (plz correct me if I am wrong).

Array: holds multiple values in a 0-based index. Function: does something cool - a little program in and of itself. Object: a thingy that hold multiple key/value pairs. Method: a function within an object.

I am just finishing up the jQuery courses in Building an interactive website. In the code challenges for the Form Validation and Manipulation section it asks me to create methods. But what I am creating is functions. However, maybe the code challenge is asking for methods because we are working in the jQuery object -- so technically every function is a method in jQuery. Is that the case?

I would appreciate some clarification on this. Thx much and happy holidays!

4 Answers

I think you're right. When you are creating a "function" via jQuery, that is actually a method. I think it's because jQuery itself is a giant function. Take a look at the first line of code here http://code.jquery.com/jquery-latest.js

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi John,

I tend to use the terms interchangeably as in most, if not all cases they're identical in meaning.

I found a stackoverflow that discusses it more, note the replies to the answer on how the definitions slightly change language to language. And I don't think this definition helps either as I'll to try explain.

You can do some pretty crazy-cool things in JavaScript like passing functions around from the global scope in to an object and vice versa. The key with JavaScript is scope of execution of the function.

For example when you define a function like this.

function hello() {
  alert("Hi");
}

//Which means I can call
window.hello();

This function is now a method on window :) Any function defined in the global scope (in browsers) gets bound to the window. Whilst we haven't implicitly passed in window we can access it and all the variables within that scope. this within the context of the hello function is the window.

function hello() {
  console.log(this); //logs out the window.
}

As for jQuery itself, Eric pointed out there's a function declared on the first line, and if you look at the end it executes itself passing in window as a variable. So you could say that this is a method (in the definition from Stack Overflow) since it's function been explicitly called with the object that's calling it has been passed in, window. For more info on self-executing anonymous functions check out this blog post but you can never call it again directly but it binds the jQuery functionality to window.jQuery / window.$ near the end.

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

/Me takes a breath

So when you call jQuery("css selector string") or $("css selector string") you're calling the jQuery method on window which is a function defined in a self-executing anonymous function.

So I find that definition proposed on StackOverflow confusing.

To be honest I wouldn't get too hung up on the definitions. Scope is what you should be focusing on not if this is strictly a method or a function, by some definition, either way they get called the same way. The context of this within the method/function is key.

In your day to day developer life if someone says called the blah function rather than blah method there is no issue, people understand these terms as interchangeable.

Eventually you'll be writing code that doesn't pollute the the global scope unnecessarily . CoffeeScript does this straight out of the box wrapping any of your code in a self-executing anonymous function when it's compiled down to JavaScript.

Let me know if I've made it any clearer or worse :)

Regards Andrew

John Dugan
PLUS
John Dugan
Courses Plus Student 13,165 Points

Totally cleared it up Andrew. Thank you so much for the thorough response.

Happy Holidays! John

Gareth Redfern
Gareth Redfern
36,217 Points

Great response @Andrew, that's a great little lesson learned ;-) there can definitely be confusing terms used quite a lot and it really helps to have a clear definition like you have given.