简体   繁体   中英

How to access the function property of a javascript object using variable

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

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.

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)

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()))

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();

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;

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM