JQuery: how to write a plugin function that takes a function name as a string and runs it?

I ran into a problem and don't know how to solve this in jQuery.

My goal is to write an advanced function that replaces the Facebook callSWF function.

Facebook callSWF run like that:   **domObj.callSWF("myFunctionName", param1, param2 ... paramx);**

Now I need to replace this function. For jQuery, I can invoke Flash via JS as follows:

if($('#swfObj')[0])
 {
  $('#swfObj')[0].myFunctionName(param1, param2 ...);
 }
 else
 {
  $('#swfObj').myFunctionName(param1,param2 ...);
 }

However, this means that I need to change for 50+ different places that previously called "callSWF" and replaced this block of code with different function names. that is nothing good.

so I decided to write an extension. Ideally, I can call it as follows:

$ ('# swfObj'). callSWF ("myFunctionName", param1, param2 ...);

I'm starting to write a function and get stuck .... here is my code:

(function( $ ){
  $.fn.callSWF = function(swfFuncName, param1) {

 if($(this)[0])
 {
  $(this)[0].**swfFuncName**(param1);
 }
 else
 {
  $(this).**swfFuncName**(param1);
 }

 return this;
  };
})( jQuery );

, , ... ( swf). swfFuncName , .

, :

**$('#swfObj').callSWF("showMyName", 'whatever');**
**$('#swfObj').callSWF("anotherFunction", 1234);**
**$('#swfObj').callSWF("yetAnotherFunction", 'kbieure');**

? .

+3
1

, "" "", $(this)[0] $(this) ( )

 if($(this)[0])
 {
  $(this)[0][swfFuncName](param1);
 }
 else
 {
  $(this)[swfFuncName](param1);
 }
+1

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


All Articles