jQuery: The Write Less, Do More JavaScript Library

Benefit of extending jQuery with a function vs. a JavaScript function?

View: New views
4 Messages — Rating Filter:   Alert me  

Benefit of extending jQuery with a function vs. a JavaScript function?

by marty.mcgee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello, Marty McGee here.  I was hoping to open a discussion about the
benefits of extending jQuery with your own custom functions versus
simply writing your functions in JavaScript and calling them without
extending jQuery first.  Please enlighten me and the rest of the
humble jQuery library addicts.  Thanks to all.

For example, this:

$(function(){
  $.function_name = function test() {
    alert('in function');
  }
});

Versus this:

$(function(){
  test();
});

function test() {
  alert('in function');
}

-------------------------------------------------------------

Re: Benefit of extending jQuery with a function vs. a JavaScript function?

by dave.methvin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Hello, Marty McGee here.  I was hoping to open a discussion about the
> benefits of extending jQuery with your own custom functions versus
> simply writing your functions in JavaScript and calling them without
> extending jQuery first.

I am not a fan of extending the jQuery object with unrelated
application-specific functionality. It avoids polluting the global
namespace, but then again it pollutes the jQuery namespace. If jQuery
later tries to claim that name for its own uses, you'll have a
conflict.

Re: Re: Benefit of extending jQuery with a function vs. a JavaScript function?

by Michel Belleville :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'd say extend jQuery when you're writing generic plugins that may be reused in another context, your application-specific code has no business extending jQuery.

Michel Belleville


2009/11/10 Dave Methvin <dave.methvin@...>
> Hello, Marty McGee here.  I was hoping to open a discussion about the
> benefits of extending jQuery with your own custom functions versus
> simply writing your functions in JavaScript and calling them without
> extending jQuery first.

I am not a fan of extending the jQuery object with unrelated
application-specific functionality. It avoids polluting the global
namespace, but then again it pollutes the jQuery namespace. If jQuery
later tries to claim that name for its own uses, you'll have a
conflict.


Re: Benefit of extending jQuery with a function vs. a JavaScript function?

by kalahari_kudu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I agree with the other respondents... rather create your own
"namespace" and encapsulate your functions there.

e.g.

var myApp = { };

myApp.myTestFunction = function (param1) {
    alert(param1);
}

myApp.myTestFunction('my function!');

Actually, don't listen to me :)  look at a few of Crockford's video's
and the patterns he talks about.  I especially like the following
example that allows you very easily build contained "modules" using
the correct scopes. (source: http://javascript.crockford.com/code.html)

var collection = (function () {
    var keys = [], values = [];

    return {
        get: function (key) {
            var at = keys.indexOf(key);
            if (at >= 0) {
                return value[at];
            }
        },
        set: function (key, value) {
            var at = keys.indexOf(key);
            if (at < 0) {
                at = keys.length;
            }
            keys[at] = key;
            value[at] = value;
        },
        remove: function (key) {
            var at = keys.indexOf(key);
            if (at >= 0) {
                keys.splice(at, 1);
                value.splice(at, 1);
            }
        }
    };
}());

On Nov 9, 6:27 pm, "marty.mcgee" <mcgee.ma...@...> wrote:

> Hello, Marty McGee here.  I was hoping to open a discussion about the
> benefits of extending jQuery with your own custom functions versus
> simply writing your functions in JavaScript and calling them without
> extending jQuery first.  Please enlighten me and the rest of the
> humble jQuery library addicts.  Thanks to all.
>
> For example, this:
>
> $(function(){
>   $.function_name = function test() {
>     alert('in function');
>   }
>
> });
>
> Versus this:
>
> $(function(){
>   test();
>
> });
>
> function test() {
>   alert('in function');
>
> }
>
> -------------------------------------------------------------