ES6 - Object.setPrototypeOf
借助此函数,我们可以将指定对象的原型设置为另一个对象或 null。
语法
在此语法中,obj 是要设置其原型的对象,prototype 是该对象的新原型(一个对象或 null)。
Object.setPrototypeOf(obj, prototype)
示例
<script> let emp = {name:'A',location:'Mumbai',basic:5000} let mgr = {name:'B'} console.log(emp.__proto__ == Object.prototype) console.log(mgr.__proto__ == Object.prototype) console.log(mgr.__proto__ ===emp.__proto__) Object.setPrototypeOf(mgr,emp) console.log(mgr.__proto__ == Object.prototype) //false console.log(mgr.__proto__ === emp) console.log(mgr.location,mgr.basic) </script>
上述代码的输出将如下所示 −
true true true false true Mumbai 5000