How to execute a method passed as parameter to function

前端 未结 8 650
旧巷少年郎
旧巷少年郎 2020-12-13 03:44

I want to write my own function in JavaScript which takes a callback method as a parameter and executes it after the completion, I don\'t know how to invoke a method in my m

8条回答
  •  别那么骄傲
    2020-12-13 04:07

    object[functionName]();

    object: refers to the name of the object.

    functionName: is a variable whose value we will use to call a function.

    by putting the variable used to refer to the function name inside the [] and the () outside the bracket we can dynamically call the object's function using the variable. Dot notation does not work because it thinks that 'functionName' is the actual name of the function and not the value that 'functionName' holds. This drove me crazy for a little bit, until I came across this site. I am glad stackoverflow.com exists <3

提交回复
热议问题