Is there a way to wrap a jquery function to an object?

user3718546

Is there anyway to limit the scope of a jquery function to an object other than using a main function:

(function( $ ) {

    $.fn.myLib = function( funcName ) {

        if ( action === "func1") {
            // Open popup code.
        }

        if ( action === "func2" ) {
            // Close popup code.
        }

    };

}( jQuery ));

I'd like to do it so that I could call functions like this:

$(el).myLib.func1();
$(el).myLib.func2();
jfriend00

There's really not a lot of good reason to try to do this:

$(el).myLib.func1();

Because the this pointer when func1() executes will be myLib and you won't be able to access $(el). So, it doesn't do you a whole lot of good unless all you want is static methods. If that's what you actually want, then you can do this:

$.fn.myLib = {};
$.fn.myLib.func1 = function(...) {...};
$.fn.myLib.func2 = function(...) {...};

If you actually want to be able to have acceess to the jQuery object (which I presume), then stick to one level and use a name prefix which gives you about the same level of name conflict protection anyway.

$.fn.myLibFunc1 = function(...) {...};
$.fn.myLibFunc2 = function(...) {...};

Then, you can do:

$(el).myLibFunc1();

And, the this pointer in myLibFunc1 will be the jQuery object that called it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to wrap the jQuery function

From Dev

Wrap jQuery plugin with a function

From Dev

Is there a way to wrap an object up as a ConcurrentObject?

From Dev

Wrap function Jquery for multiple elements

From Dev

Wrap Angular App in a jQuery function

From Dev

jQuery wrap() only works on the DOM, not on jQuery object?

From Dev

jQuery wrap() only works on the DOM, not on jQuery object?

From Dev

Cleaner way to wrap up jquery functions

From Dev

Wrap perl's sort function in an object

From Dev

Wrap an OCaml function that receives a JS object (record)

From Dev

What is the proper way to wrap a JSON object in another object?

From Dev

Using Jquery wrap function with absolute placed element

From Dev

In javascript or jquery, what's the best way of running a function whose function name and parameters come in a json object?

From Dev

Why is a function wrap unside a function object not called in javascript

From Dev

Why is a function wrap unside a function object not called in javascript

From Dev

IS there a way to wrap a Objective-C Object with a C++ Class

From Dev

jQuery function returning [Object Object]

From Dev

$.each() function jQuery on an object

From Dev

TypeError: object is not a function - jquery

From Dev

TypeError: object is not a function - jquery

From Dev

Finding the simplest way to wrap a double->double C function to Python

From Dev

jQuery wrap() in on()

From Dev

Cython: How to wrap a C++ function that returns a C++ object?

From Dev

Clone a form input as a jQuery Object And Display it with wrap div / li

From Dev

wrap set time out function around simple slide animation jquery

From Dev

How important it is to wrap jquery code in $(document).ready() function?

From Dev

Calling a javascript function on a jQuery object

From Dev

passing object to callback function in jquery

From Dev

Jquery on() click function on custom object

Related Related

  1. 1

    How to wrap the jQuery function

  2. 2

    Wrap jQuery plugin with a function

  3. 3

    Is there a way to wrap an object up as a ConcurrentObject?

  4. 4

    Wrap function Jquery for multiple elements

  5. 5

    Wrap Angular App in a jQuery function

  6. 6

    jQuery wrap() only works on the DOM, not on jQuery object?

  7. 7

    jQuery wrap() only works on the DOM, not on jQuery object?

  8. 8

    Cleaner way to wrap up jquery functions

  9. 9

    Wrap perl's sort function in an object

  10. 10

    Wrap an OCaml function that receives a JS object (record)

  11. 11

    What is the proper way to wrap a JSON object in another object?

  12. 12

    Using Jquery wrap function with absolute placed element

  13. 13

    In javascript or jquery, what's the best way of running a function whose function name and parameters come in a json object?

  14. 14

    Why is a function wrap unside a function object not called in javascript

  15. 15

    Why is a function wrap unside a function object not called in javascript

  16. 16

    IS there a way to wrap a Objective-C Object with a C++ Class

  17. 17

    jQuery function returning [Object Object]

  18. 18

    $.each() function jQuery on an object

  19. 19

    TypeError: object is not a function - jquery

  20. 20

    TypeError: object is not a function - jquery

  21. 21

    Finding the simplest way to wrap a double->double C function to Python

  22. 22

    jQuery wrap() in on()

  23. 23

    Cython: How to wrap a C++ function that returns a C++ object?

  24. 24

    Clone a form input as a jQuery Object And Display it with wrap div / li

  25. 25

    wrap set time out function around simple slide animation jquery

  26. 26

    How important it is to wrap jquery code in $(document).ready() function?

  27. 27

    Calling a javascript function on a jQuery object

  28. 28

    passing object to callback function in jquery

  29. 29

    Jquery on() click function on custom object

HotTag

Archive