Concatenate Method to Object


I want to create a function that takes two parameters: an object and a method to apply to it. See code below. Any help would be greatly appreciated!

        //A function that applies a method to an object.
        var applyMethodToVariable = function(object, method) {
            var applyMethod = object + method;
        applyMethodToVariable("Ryan", ".length()")
        //Desired result: console logs 4.
        //Result I get: "Ryan.length()".

First of all, you are not passing an object or a method to your function. You are passing two strings, so you are getting the concatenation of the two strings.

Do you understand what an object is? Do you understand what a method is?

If not, I strongly suggest you work through the Free Code Camp JavaScript Algorithms and Data Structures certification before proceeding any further.

My last post may have come across a bit too harsh. You can do what you want, but you would need to define a function that would return the length of the string.

function applyFunctionToString(str, func) {
  return func(str);

function getLengthOfString(str) {
  return typeof str === 'string'
    ? str.length
    : null;

console.log(applyFunctionToString('Ryan', getLengthOfString)); // 4
console.log(applyFunctionToString(3, getLengthOfString)); // null

My wording may not have been perfect, in which case, I do apologize. If instead of “Ryan” I passed myName (i.e. var myName = “Ryan”:wink: that would be an object. I hope people viewing my post can understand what I’m trying to convey here.

To expand:

I could get the answer by using: myName.length (if I write it out). But what if I have myName and separately methodToUse (i.e. var methodToUse = “.length”), and I want to join them in a way that returns 4 (i.e. so it behaves like myName.length) instead of returning a string “myName.length”? This is the question I’m hoping to get help with. It isn’t feasible for me to write a new function for every method I may want to use. If anyone has ideas, I’m open to anything.


Will this function be used on user input? If you can share a bit more of the purpose of such a function, I can offer a bit more guidance.

As long as the function would only be used on “controlled” input (no user input), then you could do something like:

function applyMethodToStr(str, method) {
  return eval(`"${str}"${method}`);

applyMethodToStr('Ryan', 'length'); // 4
applyMethodToStr('Ryan', '.indexOf("y")'); // 1

Just keep in mind, it is highly frowned upon to use the eval method unless it is absolutely needed. 99% of the time, there is another way to accomplish the task without using it.

NOTE: There are many edge cases of the function I wrote above that will fail in the function’s current state.

For example, if the string had any double quotes in it, the function would error out:

applyMethodToStr('"Ryan"', 'length'); // SyntaxError: Unexpected indentifier

As my understanding, you want to create a function that executes a method giving by name (second argument) on an object/variable, basically, you want to run js code from a string inside your function.

For that, you can use eval(str) function that evaluates str

function applyMethodToVar(v, method){
     // this solution is not guaranteed to work 100%
     // and I am assuming that method returns something
     let _ans;
applyMethodToVar("hello", ".length"); // should return 5

but, I don’t recommend using eval at all, because it can lead to some serious security problems, see:, so you need carefully verify your inputs

You would have to have “.length” instead of “length” here. Also, you can not use var as a parameter name, because it is reserved in JavaScript. Also, you would have to wrap the variable in quotes or you will get an error. See my example above.

1 Like