Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery equivalent of Ruby's .send() method

Tags:

jquery

I am currently developing a jQuery plugin where I would like to dismiss an element but have the way it is dismissed (slideUp, hide, and other jQuery methods) specified as an option.

For example, someone will be able to specify:

$('element').plugin({style: "slideUp", speed: 300});

and it will call slideUp on the given element.

This is a simplified example and the plugin will do a lot more but I was just wondering if something like this was possible in JavaScript / jQuery. As a parallel I am looking for something much like Ruby's .send() method.

Thanks!

EDIT: I did find this plugin which does something similar to what I am looking for but I would love if there was some way to do this without including another plugin.

like image 898
infinityrobot Avatar asked Jul 06 '12 06:07

infinityrobot


1 Answers

You can access any member of a javascript object (including functions) using the square brackets [] And then call that, with the normal parenthesis (). Here's a very simplified example

​var test = {
    slide: function() {alert('slide');}​,
    fade: function() {alert('fade');}
}

var style = "slide"; // or 'fade'

test[style]();​​​​​​
like image 90
ctcherry Avatar answered Nov 08 '22 19:11

ctcherry