While playing around with some pet projects today, I came around a certain peculiarity that I can't quite explain. Here's a log from a node repl:
> foo = Object.create({}, { toString: { value: function() { return 'bob' } } })
{}
> bar = Object.create(foo)
{}
> bar.toString()
'bob'
> bar.hasOwnProperty('toString')
false
> bar.toString = function() { return 'nope' }
[Function]
> bar.toString()
'bob'
It was my expectation that bar.toString
would shadow foo.toString
, however this does not seem to happen. Setting the toString
property to writable: true
when creating foo
makes it work as I'd expected.
Can prototype properties that aren't writable be shadowed?