0

Having the following html:

<div id="plugin">
 <script>
  (function(){

     this.do = function(e){
       alert("cool!");
     };
  });
 <script>
<div>

how do I call this.do (say from a parent element click button event handler)?

Maybe some more information will help advising on the right solution. It's a plugin, I want both markup and script to be part of the same file. I also want to be able to do something like:

$("#plugin").*controller*.do(); from outside of the plugin.

Here's a candidate solution, inspired by Scott's answer:

<div>
 (function(){
   var plugin = $("#plugin");

   plugin.do = function(e){ alert("cool!"); };

 });
</div>

then from outside:

   $("#plugin").do();

any issues with it?

6
  • 1
    you just completely changed the context of the question with that edit Commented Jun 18, 2014 at 16:16
  • @Scott Sorry if it looked like it, in fact it was probably lack of description from the beginning. I love your proposal though. Commented Jun 18, 2014 at 16:21
  • 1
    You really should have a look at the jQuery manual on how to create plugins and plugin functions: learn.jquery.com/plugins/basic-plugin-creation Commented Jun 18, 2014 at 16:22
  • man, what I'm trying to do has got nothing to do with jQuery plugins, not everything called 'plugin' is a jQuery plugin, right?;) Commented Jun 18, 2014 at 16:25
  • @user3455395 - it looks like - i'd be willing to bet you're plugin depends on jQuery - now even if I'm wrong , what does it hurt to make your new additional functionalitly better written to accept and return jquery objects? your "plugin" will be better if you follow the guidelines of creating a jQuery plugin function. Even if this one you're talking about doesn't Commented Jun 18, 2014 at 16:30

3 Answers 3

2

This looks like you can't, as the wrapper function doesn't have a name and does not self-execute. So the code never runs. If it would run, the this keyword in a global function refers to the window object and the inner function would be globally available.

(function(){

    this.do = function(e){
        alert("cool!");
    };
})(); // <-- additional parentheses to self-execute the wrapper function

window.do === do; // true

// use
do();
// or
window.do();
// to call the inner function

But having such a wrapper function looks like you want to avoid global functions and variables (which is good for a number of well documented reasons). In that case you could define do as private method of your module and refer to it inside the wrapper function.

(function(){

    var do = function(e){
        alert("cool!");
    };

    var button = document.querySelector( 'button' ); // get the first button element
    button.addEventListener( 'click', do, false );

})();

typeof do === 'undefined' // true; the function do is only available inside the wrapper function.
Sign up to request clarification or add additional context in comments.

Comments

1

why are you against just doing?

 <script>
   function do(){
      alert('cool!');
   }
 <script>

you could do :

<script>
   window.do = function(){
      ...
   }
</script>

then some where else:

window.do()

5 Comments

could I lower the scope to the plugin itself by using $("#plugin").do = function(){} - surely I should be able to call $("#plugin").fo after it?
now you want to add a function to jQuery , that can be performed on any jQuery object - you completely changed what you were trying to do , I'll make a solution for that for you
you know you can chain Jquery functions ?? .hasClass().css().removeClass() - it's because all of those functions are expecting and returning the same thing . You should just google adding function to jQuery plugin - that is probably a more appropiate answer for you.
it's not a jQuery plugin it's a plugin in concept, something which lives on its own but can be loaded and manipulated...
it sounds like... just going off on a limb here , it depends on jQuery , hence me calling it a jQuery plugin , I think Google can help here
0

You can't. The outer function expression isn't called or assigned anywhere, so it will be discarded without ever evaluating the inner function expression.

If you want to bind the function somewhere, call addEventListener on the element you want to assign it to and pass it as the second argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.