So I see plenty of JavaScript code out there that uses “new” when making constructors. After reading part of JavaScript the Good Parts it seems that using “new” isn’t the cat’s pajamas. That was 4 years ago though… Is it still not recommended? What is the current standard?
Share
Since when is
newnot recommended? D. Crockford has a valid point and a strong opinion butnewis part of the language and it’s very much being used in many projects.newis part of the prototype inheritance model, and must be used to create new instances with a constructor function. Crockford points out a purely functional approach using thethiscontext appropriately andreturn thisto be able to inherit properties and methods between children objects. It’s a different solution to a common problem, but it doesn’t mean thatnewshouldn’t be used. In fact, one of the most copy/pasted JS snippets of all times is Crockford’s, theobject.createpolyfill, and it usesnew: