Conditionally adding object methods

I stumbled my way into an interesting experiment today where I discovered that, within a JavaScript constructor function, one might conditionally add a method based on whether the prototype has that method already or not.

.gist table { margin-bottom: 0; }

This code ensures that when the method `bar` exists on the prototype for Foo, the instance implementation of `bar` added in Foo’s constructor won’t be applied. So the instance implementation is kind of like a “default implementation”.This m...

 •  0 comments  •  flag
Share on Twitter
Published on January 06, 2015 09:26
No comments have been added yet.


Nicholas Cloud's Blog

Nicholas Cloud
Nicholas Cloud isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Nicholas Cloud's blog with rss.