What's the difference between Reflect.getMetadata and Reflect.getOwnMetadata?
Asked Answered
M

1

13

As said in the title: the reflect-metadata API provides a getMetadata method and a getOwnMetadata - what's the difference here? Same goes for hasOwnMetadata, etc.

Masque answered 29/1, 2018 at 20:22 Comment(0)
P
18

Generally the different between the Own versions and the regular ones is whether the lookup continues up the prototype chain. In the own versions, only metadata defined specifically on the target object is found. In the regular versions if the metadata was not defined on the target object, metadata defined on the prototype of the object is returned.

Example :

@Reflect.metadata("key", "base value")
class B {
    get prop(): number { return 0; }
}

class C extends B{ }

// "base value", metadata was not defined on C but was defined on it's prototype B
console.log(Reflect.getMetadata("key", C)); 

// undefined, metadata was not defined on C
console.log(Reflect.getOwnMetadata("key", C)); 
Pryer answered 29/1, 2018 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.