1

In python, the syntax of getattr() method is:

getattr(object, name[, default])

The above syntax is equivalent to:

object.name

What is the equivalent of getattr in Node.js?

pouya
  • 3,400
  • 6
  • 38
  • 53
  • 1
    You mean, other than using plain dot notation? – CertainPerformance Oct 20 '18 at 08:38
  • 1
    @CertainPerformance I mean a builtin method to access an object property by its name instead of dot notation. – pouya Oct 20 '18 at 08:39
  • 3
    `object[name]` ... – zwer Oct 20 '18 at 08:40
  • 4
    Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – zwer Oct 20 '18 at 08:41
  • @zwer i like that simply works. Is it work for any object proprty. so can i do object[name](arg) for a method? – pouya Oct 20 '18 at 08:42
  • 1
    @pouya you should also see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors – Ayush Gupta Oct 20 '18 at 08:43
  • 1
    @pouya yes you can use it for a method – Ayush Gupta Oct 20 '18 at 08:47
  • 1
    @pouya - `object[name]` will get you the property `name` (or rather its value) of `object`. If that property is a function nothing stops you from calling it. It's evaluated left-to-right so you can chain as many arbitrary calls as you need. – zwer Oct 20 '18 at 08:47
  • @zwer i got my answer thanks. if you provide an answer i will aceept it. – pouya Oct 20 '18 at 08:50

0 Answers0