JQuery equivalent of Ruby.send () method

I am currently developing a jQuery plugin in which I would like to remove an element, but its rejection method (slideUp, hide and other jQuery methods) is specified as an option.

For example, someone will be able to specify:

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

and it will call slideUp for the given element.

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

Thanks!

EDIT: I found this plugin that does something similar to what I am looking for, but I would love it if there was some way to do this without including another plugin.

+6
source share
1 answer

You can access any member of the javascript object (including functions) using the square brackets [] and then call it, with the usual bracket () . Here is a very simplified example.

 โ€‹var test = { slide: function() {alert('slide');}โ€‹, fade: function() {alert('fade');} } var style = "slide"; // or 'fade' test[style]();โ€‹โ€‹โ€‹โ€‹โ€‹โ€‹ 
+9
source

Source: https://habr.com/ru/post/919820/


All Articles