Invoke a Function with the Correct Parameters from an Object in JavaScript

Kirk Ouimet

Let's say I have an object that looks like this:

{
     'apple': 'nice',
     'banana': 'decent',
     'cherry': 'yuck',
}

and I have these two methods:

function eatItems(cherry, apple) { }
function throwItem(banana) { }

My two questions:

  1. Is it possible for me to invoke eatItem and send the arguments in the correct order? Maybe something like:

    eatItems.call(this, {'cherry': cherry, 'apple': apple});

  2. What if I don't know what arguments eatItems receives, can I dynamically look up the names of the arguments for a function so I can know the order that I need to throw them in?

elclanrs

If I understand correctly you want extract the argument names from the function, and inject data from an object based on those names. This can be accomplished by converting the function to a string, extracting the arguments, and applying the function with those arguments:

function inject(data, f) {
  var args = f.toString()
    .match(/function\s*?\((.+?)\)/)
    .pop()
    .split(',')
    .map(function(a){return data[a.trim()]})
  return function() {
    return f.apply(this, args)
  }
}

var data = {
  apple: 'nice',
  banana: 'decent',
  cherry: 'yuck',
}

var eat = inject(data, function(cherry, apple) {
  console.log(cherry, apple)
})

eat() //=> yuck, nice

The obvious problem with this approach is that it is highly dependent on the variable names, so when you minify your code, the variables will get mangled and the function will stop working. This is a known problem in AngularJS, which uses something similar for their dependency injection.

This is often an XY problem, or an anti-pattern at the very least.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Invoke Javascript function from Angular

From Dev

How to invoke an object like a function in Javascript?

From Dev

Is it correct to invoke the constructor of a JavaScript Object in one of the prototype functions?

From Dev

Javascript - function with optional parameters as object?

From Dev

Javascript object function parameters performance

From Dev

marklogic invoke function with parameters

From Javascript

Javascript dynamically invoke object method from string

From Dev

Invoke a javascript function of a website from java code?

From Dev

Unable to invoke javascript function from blazor component

From Dev

Invoke an aws lambda function from another function with SQSEvent object

From Dev

correct way to invoke a function in OCaml

From Dev

Is this the correct way to use the invoke function?

From Dev

Pick correct function signature from object

From Dev

Call Javascript function with Object AND array parameters

From Dev

pass event object and other parameters to a JavaScript function

From Dev

Default destructed function parameters in one object in JavaScript

From Dev

Typescript invoke function from object index with generic type

From Dev

Cannot invoke c++ method from qml: "Property of object is not a function"

From Dev

Dynamic Typescript function parameters from object properties

From Dev

Invoke a function from a procedure

From Dev

How to invoke javascript function on web page from chrome extension?

From Dev

Can't invoke function from external javascript file

From Dev

Invoke javascript function in parent page from child page

From Dev

Puppeteer to invoke javascript function from an external .js file

From Dev

function parameters from variables fail as string javascript

From Dev

How to send parameters to Action from javascript function?

From Dev

Call Javascript function with parameters from iOS ObjC

From Dev

How to send parameters from razor to to JavaScript function

From Dev

Destructoring object during function invoke

Related Related

  1. 1

    Invoke Javascript function from Angular

  2. 2

    How to invoke an object like a function in Javascript?

  3. 3

    Is it correct to invoke the constructor of a JavaScript Object in one of the prototype functions?

  4. 4

    Javascript - function with optional parameters as object?

  5. 5

    Javascript object function parameters performance

  6. 6

    marklogic invoke function with parameters

  7. 7

    Javascript dynamically invoke object method from string

  8. 8

    Invoke a javascript function of a website from java code?

  9. 9

    Unable to invoke javascript function from blazor component

  10. 10

    Invoke an aws lambda function from another function with SQSEvent object

  11. 11

    correct way to invoke a function in OCaml

  12. 12

    Is this the correct way to use the invoke function?

  13. 13

    Pick correct function signature from object

  14. 14

    Call Javascript function with Object AND array parameters

  15. 15

    pass event object and other parameters to a JavaScript function

  16. 16

    Default destructed function parameters in one object in JavaScript

  17. 17

    Typescript invoke function from object index with generic type

  18. 18

    Cannot invoke c++ method from qml: "Property of object is not a function"

  19. 19

    Dynamic Typescript function parameters from object properties

  20. 20

    Invoke a function from a procedure

  21. 21

    How to invoke javascript function on web page from chrome extension?

  22. 22

    Can't invoke function from external javascript file

  23. 23

    Invoke javascript function in parent page from child page

  24. 24

    Puppeteer to invoke javascript function from an external .js file

  25. 25

    function parameters from variables fail as string javascript

  26. 26

    How to send parameters to Action from javascript function?

  27. 27

    Call Javascript function with parameters from iOS ObjC

  28. 28

    How to send parameters from razor to to JavaScript function

  29. 29

    Destructoring object during function invoke

HotTag

Archive