Link to home
Start Free TrialLog in
Avatar of stargateatlantis
stargateatlantis

asked on

convert this namespace into jQuery

I have the following code but how would you convert it into a jQuery namespace

JQUERY4U = {
    multiply: function(x,y) {
        return (x * y);
    }
}
//function call
JQUERY4U.multiply(2,2);

Open in new window

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Use the jQuery.fn.extend method:
jQuery.fn.extend({
    multiply: function(x,y) {
        return (x * y);
    }
});

Open in new window


or you can declare your new method as:
$.fn.multiply= function(){
  return (x * y);
}

Open in new window


Note: The first alternative is more flexible and reusable as discussed below:
http://richardneililagan.com/2011/07/using-jquery-extend-to-make-even-more-powerful-cached-objects/
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial