r/javascript • • Nov 03 '11

jQuery vs Plain old javascript

http://raynos.org/blog/14/jQuery-vs-POJS
0 Upvotes

53 comments sorted by

View all comments

Show parent comments

3

u/Wolfy87 Nov 03 '11

Sure. This method of encapsulating your script essentially sandboxes your code. All code within the wrapper will be run but any variables / functions will not leak out into the global variable namespace unless you specifically tell them to.

Heres a little example.

(function(globalObject) {
    function foo() {
        // This function is private to this wrapper
        // It can only be called from inside this wrapper
    }
    
    // This line will drop our foo function into the global object
    // This works because we are passing `this` to our anonymous function
    // As long as it is not nested then `this` will equal `window` or `exports` depending on your JavaScript environment
    globalObject.foo = foo;
}(this));

It can be a little confusing. But this helps you to create more modular code that will not pollute the browser with random variables and functions.

Just ask if you still don't get something :)

2

u/SkeeballAvenger Nov 03 '11 edited Nov 03 '11

Thanks for the assistance! Okay, I think I understand, let's see if I have this right.

The (function(globalObject)...) syntax is an anonymous function which is being called (in this example) with "this" passed as an argument, and assuming a typical browser context, would be the window object. In a node.js environment it would be the 'exports' global object.

Then, the global object is being extended with the up-until-now- private function "foo", so that we can call foo() outside of the wrapper. This looks to me like a closure, but I'm not sure that it's entirely accurate to call it that.

I do, however, understand this syntax much better, thank you! I'm saving this comment away to review again a few times to make sure I've really got it.

I'd like to ask, I've seen "namespacing" done by assigning an object to a variable like so:

var myThing = {
    foo: function () {
               // Do things here
           },
    bar: new Array;
};

Is there a reason to use the former over the latter, or is it more a matter of preference?

Thanks so much, I feel like I'm overcoming a big knowledge hurdle here. I really should find a good reference for learning to code in this style, because it feels much more modern to me than what I've been learning.

2

u/Wolfy87 Nov 03 '11

You hit the nail on the head! Thats exactly how it works.

I have never used name spacing myself in my JavaScript but I suppose the major libraries are prime examples. jQuery sticks everything in the jQuery global and creates an alias of $. MooTools splits the whole library up into multiple global objects such as Fx and Request and performs a lot of extension of existing globals. Element for example.

I think if you are trying to write modular code that can just be bolted together then name spacing is a great idea, it just helps to stop a conflict. I think it also helps as a kind of source code annotation. You can see that ui.buttons.Submit will be a button that will submit something and is part of the user interface.

I have experimented with name spacing in a script I wrote called Package.js. But that is all about checking if a script has been loaded via a namespace and loading it if not. Like Java's import. Well, I say like it, it is no where near as good.

So they are both good, they both keep code separate and clean. I would suggest using name spacing for larger projects and more reusable code. But I would use name spacing in conjunction with the sandboxing wrapper, just so you can define private functions etc. It also allows things like Google Closure Compiler to minify your code better.

2

u/SkeeballAvenger Nov 04 '11

Awesome,, thanks so very much for taking the time to spell this out for me, it's been more help than you know... For the first time in days I feel like I'm actually improving the quality of my code.

The distinction between name spacing and (whatever the proper name is for the techinique you've explained to me above is) interests me, I think I need to go play a bit and drop some code through a profiler to see if it makes any difference as well. I can certainly see the benefit of creating a new object though like the "big guys" do, too.

I will admit to being a jQuery guy, although it's more for convenience than anything (selectors). I suppose I just need to get off my duff and learn more about mootools and perhaps roll a few of my own complex selectors, to get over it.

Thanks again Wolfy!