Timeline for answer to What is the 'new' keyword in JavaScript? by Daniel Howard
Current License: CC BY-SA 4.0
Post Revisions
66 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 12, 2023 at 3:56 | history | edited | Safwan Samsudeen | CC BY-SA 4.0 |
improve formatting
|
| Dec 17, 2019 at 21:28 | comment | added | Zac Taylor | This lacks an explanation of what calling a constructor function without new does... If you're wondering what the answer is see below. | |
| Jul 14, 2019 at 11:29 | comment | added | Jose V | This answer, in conjunction with this other answer to How does JavaScript .prototype work? really made it clear for me! | |
| Dec 25, 2018 at 14:57 | history | edited | DaveFar | CC BY-SA 4.0 |
added missing word
|
| Jan 4, 2018 at 22:19 | history | edited | Jason Sears | CC BY-SA 3.0 |
Removed comma splice
|
| Feb 24, 2017 at 10:50 | comment | added | Karan Kaw | 2ality.com/2014/01/new-operator.html - This Explains it - No Fuss , No Confusion | |
| Aug 7, 2016 at 9:08 | history | edited | T.J. Crowder | CC BY-SA 3.0 |
`null` isn't primitive, so change that wording slightly
|
| Jul 9, 2016 at 5:49 | history | edited | Dhruv Chadha | CC BY-SA 3.0 |
added 71 characters in body
|
| Jul 8, 2016 at 7:33 | history | edited | Dhruv Chadha | CC BY-SA 3.0 |
deleted 40 characters in body
|
| Jul 8, 2016 at 7:26 | history | edited | Dhruv Chadha | CC BY-SA 3.0 |
added 2 characters in body
|
| Jul 8, 2016 at 7:19 | history | edited | Dhruv Chadha | CC BY-SA 3.0 |
added 19 characters in body
|
| May 9, 2016 at 6:53 | comment | added | kernix | Great explanation - missing only an example of what happens when returning a new object from the Construction function. | |
| May 4, 2016 at 23:52 | comment | added | wbyoung | @masterxilo yes, the changes that I made on 3/6/14 are in line with what you're saying. | |
| May 1, 2016 at 1:42 | comment | added | masterxilo |
@wbyoung You misunderstood. It is not a copy, it is a reference to the same object Constructor.prototype is referencing. An assignment Constructor.prototype = {...} can make the constructor reference a different prototype object later on, to be used in subsequent constructions, but the object [[prototype]] referenced by the already created objects stays the same.
|
|
| Nov 9, 2015 at 12:33 | history | edited | Jonathan | CC BY-SA 3.0 |
It is a common convention to Capitalize constructor functions in JS
|
| Jan 10, 2015 at 2:51 | history | edited | Josh Rieken | CC BY-SA 3.0 |
added 158 characters in body
|
| Jan 8, 2015 at 17:37 | history | edited | user3310334 | CC BY-SA 3.0 |
formatted code for cross-browser wrapping, and clarified a little
|
| Jul 8, 2014 at 14:23 | history | edited | Quentin | CC BY-SA 3.0 |
deleted 456 characters in body
|
| Jun 10, 2014 at 7:01 | comment | added | Verron Knowles | Oh my. In all my years of using javascript, I never knew this. Also the 'so java checks its prototype' is a live check. For example, if Test2 is constructed from Test1 beforehand, and Test1 adds a new function afterwards, Test2 still has access to that function. Simply amazing. | |
| Jun 4, 2014 at 9:41 | comment | added | rene | @tomp I found a nice blog explaining the problem a little bit more. | |
| May 21, 2014 at 10:11 | comment | added | yuri | Here another detailed explanation about creating new objects. It helps me understanding very well differences about object creation, the constructor property, prototype chaining: davidwalsh.name/javascript-objects-deconstruction | |
| May 14, 2014 at 8:41 | history | edited | sleske | CC BY-SA 3.0 |
explain "constructor function"
|
| Mar 6, 2014 at 19:47 | history | edited | wbyoung | CC BY-SA 3.0 |
Improved readability
|
| Mar 6, 2014 at 19:42 | comment | added | wbyoung | I found the following information that explains how [[prototype]] or proto work here and updated the answer to reflect the fact that [[prototype]] is not actually copied. @silasdavis I'd love for you to review these changes. | |
| Mar 6, 2014 at 19:40 | history | edited | wbyoung | CC BY-SA 3.0 |
Fixed inaccurate statements about copying (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto)
|
| Mar 6, 2014 at 19:17 | comment | added | wbyoung |
@silasdavis is there an example that you can give that shows that [[prototype]] is a copy of Constructor.prototype at the time that new is used? Either I'm not understanding or something different is happening from what you're explaining.
|
|
| Mar 6, 2014 at 18:57 | history | edited | wbyoung | CC BY-SA 3.0 |
Changed phrasing… "notice that" calls attention as if you're supposed to read it and it was originally important. "note" feels less compelling.
|
| Mar 6, 2014 at 18:48 | comment | added | wbyoung |
@tomp SubObjMaker.prototype = Object.create(ObjMaker.prototype); and you may need a shim for Object.create.
|
|
| Feb 17, 2014 at 12:18 | comment | added | Tom Pažourek |
There is a note that says Notice that this pattern is deprecated!. What is the correct up-to-date pattern to set the prototype of a class?
|
|
| Jan 12, 2014 at 14:07 | history | edited | Bergi | CC BY-SA 3.0 |
added 43 characters in body
|
| Dec 31, 2013 at 12:16 | history | edited | Chai | CC BY-SA 3.0 |
corrected spelling
|
| Nov 30, 2013 at 10:26 | history | edited | Alp | CC BY-SA 3.0 |
edited body
|
| Oct 23, 2013 at 15:18 | review | Suggested edits | |||
| Oct 23, 2013 at 15:31 | |||||
| Oct 10, 2013 at 18:07 | history | edited | xdhmoore | CC BY-SA 3.0 |
fixed grammar
|
| Sep 30, 2013 at 20:20 | review | Suggested edits | |||
| Sep 30, 2013 at 20:26 | |||||
| Aug 27, 2013 at 23:59 | history | edited | plalx | CC BY-SA 3.0 |
Added missing information related to the return value
|
| Aug 27, 2013 at 20:30 | history | edited | Bolo | CC BY-SA 3.0 |
deleted 1 characters in body
|
| S Aug 8, 2013 at 9:44 | history | suggested | Havvy | CC BY-SA 3.0 |
Fixing inaccuracy on setting and getting internal [[prototype]] property.; Post Made Community Wiki
|
| Aug 8, 2013 at 9:33 | review | Suggested edits | |||
| S Aug 8, 2013 at 9:44 | |||||
| Aug 4, 2013 at 11:40 | comment | added | Robert Massaioli | I ran a little JSFiddle test to see how the this object works in relation to the new keyword if anybody is interested: jsfiddle.net/yux2a/1 | |
| Jun 6, 2013 at 2:04 | comment | added | charlie roberts | a small point to add: a call to a constructor, when preceded by the new keyword, automatically returns the created object; there is no need to explicitly return it from within the constructor. | |
| Apr 23, 2013 at 14:28 | comment | added | Kamal Reddy | @hvgotcodes you can get complete js internals here. ecma-international.org/publications/standards/Ecma-262.htm | |
| Jan 15, 2013 at 4:45 | history | edited | Igor | CC BY-SA 3.0 |
deleted 2 characters in body
|
| Oct 23, 2012 at 23:32 | comment | added | Engineer | @LonelyPixel ...And when using new, the meaning of this is modified to refer to the object just created via new -- exactly as when you create an object using brace-format (a la JSON) without new. So again, new can be completely circumvented. It has been stated elsewhere that new is actually a bit slower than creating an empty object. In any case, the future is Object.create(). I think it's fair to say that what you're already doing is probably similar to Crockford's explicit Object.create() from before it was added to the ES5 standard. | |
| Oct 23, 2012 at 22:36 | comment | added | Engineer |
@LonelyPixel new exists so that you don't have to write factory methods to construct/copy functions/objects. It means, "Copy this, making it just like its parent 'class'; do so efficiently and correctly; and store inheritance info that is accessible only to me, JS, internally". To do so, it modifies the otherwise inaccessible internal prototype of the new object to opaquely encapsulate the inherited members, mimicking classical OO inheritance chains (which aren't runtime modifiable). You can simulate this without new, but inheritance will be runtime modifiable. Good? Bad? Up to you.
|
|
| Oct 23, 2012 at 21:25 | comment | added | ygoe |
@NickWiggill He uses this, I don't.
|
|
| Oct 23, 2012 at 17:14 | comment | added | Engineer | @LonelyPixel Maybe if you looked at JulianR's equally-excellent answer, below. | |
| Oct 14, 2012 at 23:22 | comment | added | sanz | Doesn't the new keyword also set the 'constructor' property of the instance to the constructor function | |
| Sep 24, 2012 at 21:09 | comment | added | ygoe |
That's all good and interesting, but it doesn't answer an important part of the question. Especially, when the use of new is appropriate and when not. I can write a function that returns an object with functions, just like you can write a "class" definition. I can use methods of such an object when I use var obj = new func() as well as just var obj = func(), e.g. obj.method() works fine in both cases. It's the same to me right now. I don't use the this keyword though, I don't need it. Do I need new?
|
|
| S May 30, 2012 at 19:51 | history | suggested | corazza | CC BY-SA 3.0 |
improved formatting
|
| May 30, 2012 at 19:26 | review | Suggested edits | |||
| S May 30, 2012 at 19:51 | |||||
| S May 29, 2012 at 11:24 | history | suggested | Silas Davis | CC BY-SA 3.0 |
As George Mauer comment states when a new object is created from a base object the prototype must be copied to the new object not shared between itself and the base for the inheritance to work as expected.
|
| May 29, 2012 at 10:53 | comment | added | Silas Davis |
Perhaps that's not quite right.. An instance under prototype inheritance is exactly an object whose prototype contains the prototype of a base object; a might diverge from being an instance of A after being created var a = new A(), and var b = {} might converge into being an instance of A via b.__proto__ = A.prototype. In any case I don't think there is a chain of references maintained like in a class inheritance, it is all by virtue of the copying (and overwriting) of the prototype.
|
|
| May 29, 2012 at 10:41 | comment | added | Silas Davis | George Mauer is right, only I think it is better to think of 'creating a new object from a base object' and 'copying the base object's prototype to the new object', than of creating a 'new instance', which is a concept that really belongs to class-based inheritance, not prototype-based inheritance. I have hopefully edited the question to reflect this... | |
| May 29, 2012 at 10:39 | review | Suggested edits | |||
| S May 29, 2012 at 11:24 | |||||
| May 22, 2012 at 18:06 | comment | added | Cemo |
Note that ObjMaker = function() {this.a = 'first';}; when you calling just ObjMaker(), you are assigning first to global object, usually window, but when you call new ObjMaker(), this is pointing current object so you can call new ObjMaker().a
|
|
| S Mar 11, 2012 at 16:09 | history | suggested | Anish Gupta | CC BY-SA 3.0 |
improved formatting and improved grammar
|
| Mar 11, 2012 at 15:20 | review | Suggested edits | |||
| S Mar 11, 2012 at 16:09 | |||||
| Feb 27, 2012 at 19:05 | comment | added | Jim Blackler |
Question: what happens differently if ObjMaker is defined as a function that returns a value?
|
|
| Feb 1, 2012 at 20:47 | history | edited | Ufuk Hacıoğulları | CC BY-SA 3.0 |
edited comments to make it more readable
|
| Aug 31, 2011 at 22:14 | comment | added | Art | You can actually access the internal prototype object of the object via __ proto __ or this.constructor.prototype. | |
| Jun 22, 2011 at 14:19 | vote | accept | Alon Gubkin | ||
| May 11, 2011 at 0:39 | comment | added | artur | I think this is also explained in the classic MDC docs. | |
| Apr 14, 2011 at 14:55 | comment | added | Blub | Just wanted to add: There is in fact a way to access the internal [[prototype]], by __proto__. This is however non-standard, and only supported by relatively new browsers (and not all of them). There is a standardized way coming up, namely Object.getPrototypeOf(obj), but it is Ecmascript3.1, and is itself only supported on new browers - again. It is generally recommended to not use that property though, stuff gets complicated real fast inside there. | |
| Jan 10, 2011 at 20:56 | comment | added | hvgotcodes | @daniel, +1 for goodness. How did you find out what new does? | |
| Sep 7, 2010 at 12:30 | history | answered | Daniel Howard | CC BY-SA 2.5 |