Solved

convert this namespace into jQuery

Posted on 2013-12-01
2
215 Views
Last Modified: 2013-12-03
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

0
Comment
Question by:stargateatlantis
2 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
Comment Utility
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/
0
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
Comment Utility
Hi,

could look like this:
var JQUERY4U = {}; 
;(function($) {

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

Open in new window

(adjusted from http://www.jquery4u.com/javascript/jquery-function-namespacing-plain-english/)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now