The handler.setPrototypeOf function yields a Boolean result of true if the [[Prototype]] has been altered successfully. If the change is unsuccessful, it will return false. This function serves as a trap for Object.setPrototypeOf.
Syntax
Example
setPrototypeOf: function(target, prototype)
Parameters
target : The target object.
prototype : The object's new prototype or null.
Return value
Return a Boolean type value.
Browser Support
| Chrome | Compatibility unknown |
|---|---|
Edge |
Compatibility unknown |
| Firefox | 49 |
| Opera | Compatibility unknown |
Example 1
Example
<script>
var pro = { f:13}
var proxy = new Proxy(pro, {
setPrototypeOf(target, newProto) {
return newPrototype in target;
}
});
document.writeln('f' in proxy);
//expected output: true
</script>
Output:
Example 2
Example
<script>
var soo={
foo:1
}
var proxy = new Proxy(soo, {
setPrototypeOf(target, newProto) {
}
});
document.writeln('a' in proxy);
//expected output: false
</script>
Output:
Example 3
Example
<script>
var xyz = new Proxy({}, {
setPrototypeOf(target, newProto) {
if(key == "a") return true;
return ;false
}
}
)
var first= ("f" in xyz)
var last = ("g" in xyz)
document.write(""+first)
//expected output:false
document.write('<br/>');
document.write("" +last)
//expected output: false
</script>
Output:
Output
false
false