tags:

views:

23

answers:

1

Trying to "extend" myStuff object with a new method in a plugin-style fashion, but when I call the new plugin's method ($.validate.newthing()), I get a function does not exist error for it.

How can I properly extend myStuff with the "validation" plugin below?

<script type="text/javascript">
    var myStuff = function(){
        return {
          thingy: function() {
          // Stuff
          }
        }
    }();


    (function($){
      $.validate = function() {
        return {
          newthing: function(a) {
            return a + a;
          }
        }
      }
    })(myStuff);


    (function($) {

      console.log($.validate.newthing(2));

    })(myStuff);
</script>
+1  A: 

In your example, validate is a function that, when called, returns an object containing the newthing function. You need to replace this:

console.log($.validate.newthing(2));

With this:

console.log($.validate().newthing(2));
Brian Campbell