javascript - How to call functions that are nested inside a JQuery Plugin? -


My goal is to be able to call those functions that are inside my JQuery plugin

What is the correct syntax?

For example, this does not work:

  & lt; A href = "#" id = "click_me" & gt; Click me & lt; / A & gt; & Lt; Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Script & gt; (Function ($) {$ .fn.foo = function} {do_stuff = function () {console.log ("Hello world!"); // works do_other_stuff = function (warnings ("Who are you? ");}} // function} // function}) (jQuery); $ ("Body") foo () .; $ ("# Click_me") Click (function () {$ .fn.foo.do_stuff.do_other_stuff (); // does not work}};   

When you assign tasks without the var key, they overwrite local variables that are added to that name or global namespace. Do_stuff is a global function, which you do not want)

One way to do what you want to do is to clearly specify where you want to do your work .

  (function ($) {$ .fn.foo = function (options) {// whatever $ (.) Should be foo ()} $ $ .fn.foo.do_stuff = Function () {console.log ("hello world!");}; $ .fn.foo.do_stuff.do_other_stuff = function ({warning ("who are you?");}}}} (); < / Code> 

edit :

This works because JavaScript has all the function objects, which means that you have any arbitrary asset You can specify the value.

If you want to use variables of other functions, You can transfer the definitions inside:

  $ .fn.foo.do_stuff = function () {console .log ("Hello world!"); $ .fn.foo.do_stuff.do_other_stuff = function () {Warning ("Who are you?"); }; };  

But this would mean that the function only once you run the second function, and each time you run the function, it will overwrite the final definition. Perhaps another would be the ideal solution that each function will bring an object back to the nested function such as:

  (function ($) {$ .fn.foo = function ( Options) {// whatever $ (.) Foo () should do var do_stuff = function (do_stuff_args) {console.log} ("Hello world!"); // works with option and do_stuff_args var do_other_stuff = function ( Other_args) {alert ("who are you?"); // here you have access to options, do_stuff_args, and other_args}, return {do_other_stuff: do_other_stuff};}; return {do_stuff: do_stuff}};}) ;  

and call it using

  foo (). Do_stuff (some_options) .do_other_stuff (other_options);  

or

  var a = foo (stuff) .do_stuff (some_options); A.do_other_stuff (foo); A.do_other_stuff (bar);  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -