Skip to content

Instantly share code, notes, and snippets.

@valueof
Created May 30, 2012 17:33
Show Gist options
  • Save valueof/2837820 to your computer and use it in GitHub Desktop.
Save valueof/2837820 to your computer and use it in GitHub Desktop.
Without running this code could you tell me its output?
Object.defineProperty(Object.prototype, "foo", {
set: function (v) {
console.log("hello:", v);
},
configurable: true}
);
var obj = { foo: "bar" };
var obj2 = {};
obj2.foo = "bar2";
@isaacs
Copy link

isaacs commented May 31, 2012

hello:bar2

(EDIT: removed the space. Twas a typo.)

@medikoo
Copy link

medikoo commented May 31, 2012

Defining in literal never fallbacks to prototype, it's always about setting own properties. var obj = { foo: "bar" } is same as:

var obj = Object.create(Object.prototype, {
  foo: { value: 'bar', configurable: true, enumerable: true, writable: true }
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment