0

Hi consider below code

 let test={
    a:function(){
      console.log('Hi')
    }
  }

Is there a way to invoke property 'a' using a variable ?

This works ,

 let test={
    a:function(tmp){
      console.log(tmp)
    }
  }
let functn='a';  
test[functn]("hello");

but is there anyway to invoke the same like below code :

 let test={
    a:function(){
      console.log('Hi')
    }
  }
let functn='a("hello")';  
test.function;

The actual use case:

This is protractor system test related question,

I have a parent object with css locator [id=1] and the child elements has the locators [id=1]>div , [id=1]>span etc.

so currently parent element is stored as

let parent = element(by.css('[id=1']) , 

child as

let child1= element(by.css('[div]') 

So to find all child elements of the parent element the function is :

element(by.css('[id=1']).element(by.css('[div]')

so instead of writing the locator again, i want to achieve:

parent.child1
8
  • b = test.a; b()? Though this won't work, if the method contains this keyword. Commented Jan 7, 2020 at 15:47
  • You mean like const fnct = {name: "a", args: ["hello"]}? With that, you can do test[fnct.name].apply(null, fnct.args). But no, a("hello"); will never work. Commented Jan 7, 2020 at 15:49
  • @Bergi i updated the code , 'a("hello")' Commented Jan 7, 2020 at 15:51
  • So you want to executed a piece of JavaScript that is inside a string? Why? This sounds like an XY problem. What are you trying to solve by doing it like that? Commented Jan 7, 2020 at 15:53
  • hi this protractor test related , i have a parent object with css locator [id=1] and the child elements are [id=1]>div , [id=1]>span etc. so currently parent element is stored as let parent = element(by.css('[id=1']) , child as let child1= element(by.css('[div]') Commented Jan 7, 2020 at 15:56

6 Answers 6

1

This works ,

Yes, it does. Do that.

but is there anyway to invoke the same like below code :

No.

let functn=a("hello"); calls the function in the variable a and assigns its return value to functn.

Assuming that doesn't fail (because a isn't declared), then test.function looks at the value of the property named function (which has nothing to do with the variable function you just declared) on the object stored in test and does nothing with it.

Sign up to request clarification or add additional context in comments.

Comments

1

Was able to make it work, i am converting everything to string and calling it using eval function:

     let test={
        a:function(tmp){
          document.write(tmp)
        }
      }
    let functn='a("hello")';
    eval(Object.keys({test})[0]+'.'+functn) 

Comments

0

If in case you are looking for a currying solution:

 let test={
    a:function(str){
      return () => console.log(str)
    }
  }
let functn=test.a("hello");  
functn();

Comments

0

At first you are passing a string which is not neccessary if you dont accept any parameters. What you can do is using getters.

Documentation: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/get

let test={
   get a (){
     console.log('Hi')
   }
}

So every time you are trying to access a it will be executed and so will print you 'Hi' in your console.

test.a;

Comments

0

There is and simpler form of the code

// Using Function Arrow
let test = (message => 'Hello World!')()

console.log(test)

// Using factory function

let data = function() {
    return {
        nome:'Jack'
    }
}

console.log(data())
console.log(Object.values(data()))

Comments

0

You could change your approach to one without using eval by taking an array of key and parameter and a function which take the key and parameter and calls the function of the object.

let call = ([key, parameter]) => test[key](parameter),
    test = {
        a: function(tmp) { console.log(tmp); }
    },
    parameters = ['a', 'hello'];

call(parameters);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.