C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Reflect.setPrototypeOf() MethodThe static Reflect.setPrototypeOf() method is used to set the prototype of a specified object to another object. The first argument is the object reference and the second argument can be either null or an object. This method is same as Object.setPrototypeOf() method. Syntax:Reflect.setPrototypeOf(obj, prototype) Parameters:Obj: It is the target object of which to set the prototype. Prototype: It is the object's new prototype. Return value:This method returns a Boolean which indicates whether or not the prototype was successfully set. Exceptions:A TypeError, if the target is not an Object Browser Support:
Example 1const object = {}; console.log(Reflect.setPrototypeOf(Object.freeze(object), null)); Output: False Example 2var Animal = { speak() { console.log(this.name + ' makes a noise.'); } }; class g { constructor(name) { this.name = name; } } Reflect.setPrototypeOf(g.prototype, Animal); // If you do not do this you will get a TypeError when you invoke speak var d = new g('Mitzie'); d.speak(); Output: "Mitzie makes a noise." Example 3let toyota = { drive() { return 'value'; } } let obj = { wifi() { return 'answer'; } } Object.setPrototypeOf(obj, toyota); console.dir(obj); document.write(obj.wifi()); document.writeln("<br/>"); document.writeln(obj.drive()); Output: answer value
Next TopicJavaScript Reflect
|