0

I have this object:

 { tag: 'button',
   attrs: { onClick: this.myFunc },
   children: 'Name' }

This object is in a context where this.myFunc is a function. How can I get this.myFunc?

If I try with: obj['attrs']['onClick'] it prints all of the function:

function(event){...}
5
  • Are you trying to call the function? Commented Jun 25, 2016 at 2:15
  • @kinakuta no, I want get as string: "this.myFunc" Commented Jun 25, 2016 at 2:17
  • Well, I'm not sure how you're going to get it as a string unless you convert the object to JSON then parse it to find what follows the "onClick:" portion of the string Commented Jun 25, 2016 at 2:25
  • 1
    @kinakuta For good reasons JSON will not support functions. Commented Jun 25, 2016 at 2:48
  • Why are you trying to do so? Where / how is the object defined? As far as described it will not be possible. Commented Jun 25, 2016 at 2:51

2 Answers 2

3

Have you tried this to get the function name as a string

obj.attrs.onClick.name;

This would most likely return "myFunc" and not "this.myFunc" though, just depends what you want.

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

1 Comment

@joe I reverted your edit because 'String' vs 'string' is not a grammar issue in JavaScipt, but they refer to different types with 'string' being the primitive and most likely correct one here
0

If you want just to get the name, you may want to try this:

var name = /function ([^(]*)/.exec( obj['attrs']['onClick']+"" )[1];

1 Comment

Try it now please @granmirupa

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.