简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM