Skip to content

Instantly share code, notes, and snippets.

@johnjbarton
Created May 5, 2014 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnjbarton/30c3e72d51d9d64e36d1 to your computer and use it in GitHub Desktop.
Save johnjbarton/30c3e72d51d9d64e36d1 to your computer and use it in GitHub Desktop.
Proxy .__proto__ vs getPrototypeOf
<script src="../third_party/harmony-reflect/reflect.js"></script>
<script name="testObjectCreate">
(function() {
var aPrototype = {foo: 'foo'};
function createProxy(obj) {
return Proxy(obj, {
get: function(target, name, receiver) {
console.log(' (Proxy handler \'get\' called for name = ' + name + ')');
if (name === '__proto__')
return Object.getPrototypeOf(obj);
else
return Reflect.get(target, name, receiver);
}
});
};
var aProxy = createProxy(aPrototype);
var hasProxyAsProto = Object.create(aProxy);
console.log('hasProxyAsProto.__proto__: ',
hasProxyAsProto.__proto__);
console.log('hasProxyAsProto.__proto__ === aProxy ',
hasProxyAsProto.__proto__ === aProxy);
console.log('Object.getPrototypeOf(hasProxyAsProto) === aProxy ',
Object.getPrototypeOf(hasProxyAsProto) === aProxy);
console.log('hasProxyAsProto.__proto__ === aProxy.__proto__ ',
hasProxyAsProto.__proto__ === aProxy.__proto__);
console.log('--- Contrast with non-proxy results: ---')
var hasAPrototypeAsProto = Object.create(aPrototype);
console.log('hasAPrototypeAsProto.__proto__',
hasAPrototypeAsProto.__proto__);
console.log('hasAPrototypeAsProto.__proto__ === aPrototype ',
hasAPrototypeAsProto.__proto__ === aPrototype);
console.log('Object.getPrototypeOf(hasAPrototypeAsProto) === aPrototype ',
Object.getPrototypeOf(hasAPrototypeAsProto) === aPrototype);
console.log('hasAPrototypeAsProto.__proto__ === aPrototype.__proto__ ',
hasAPrototypeAsProto.__proto__ === aPrototype.__proto__);
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment