Aman jaura
Aman jaura

Reputation: 201

Reference Error in javascript plugin

I am creating some basic plugin and i am getting Reference error. Below is my code

jQuery.fn.validate = function(options) {
  var _self = this;
   // with below call I gets reference error.
  abc();  

    //but if call in below it works fine
  _self.on("submit", function(event) {
     abc();  // works fine
  }),

 abc = function () {
   console.log('here);
 }
};

Can someone explain why I am getting this error and how to overcome it. As i need to call some reset and init functions at the begining of the plugin.

Upvotes: 0

Views: 39

Answers (1)

user229044
user229044

Reputation: 239301

It seems like you're expecting abc to be hoisted, but you're specifically using a syntax that leaves abc undefined until the assignment is executed.

You need to move abc = function ... up above the invocations of abc(), or define the function using function abc() { } which will allow it to be hoisted above your invocations.

Note that, if you simply move the assignment, you should use var abc = function ... and create a local variable, rather than the global abc variable you're currently creating.

Upvotes: 1

Related Questions