Skip to content

Comment on Advanced objects in JavaScriptparent

Comments

Regarding your first question, it isn't necessary to define properties with that notation at all. You could just do:

  foo.foo = "bar";
But then it would be writable and configurable.
  Object.defineProperty(foo, 'bar', { value: 'baz' });
This foo.foo cannot be deleted. It cannot be written to. So it's a little different. But really I was just demonstrating the use of Object.defineProperty in a non-verbose way. You can try this out yourself, I made a jsfiddle: http://jsfiddle.net/btipling/q1v3fn20/

Regarding your second question, defineProperty is a method on Object, not Object.prototype. "Instances" as you might call them only have direct access to methods found on their prototype chain. You can think of Object.defineProperty as a static method on Object if that helps. I wrote a post about "this" which includes some information about prototypes:

http://bjorn.tipling.com/all-this

You should probably first checkout the mdn documentation though: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.