TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Reflect setPrototypeOf() Method

JavaScript Reflect setPrototypeOf() Method with examples on javascript reflect methods, apply() construct(), defineProperty() method, deleteProperty(), get(), getOwnPropertyDescriptor(), getPrototypeOf(), has(), isExtensible(), ownKeys(), setPrototypeOf() etc.

<< Back to JAVASCRIPT

JavaScript Reflect.setPrototypeOf() Method

The 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:

Chrome 49
Edge 12
Firefox 42
Opera 36

Example 1

const object = {};
console.log(Reflect.setPrototypeOf(Object.freeze(object), null));

Output:

False

Example 2

var 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 3

let 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf