Something that’s been bugging me for a while is the difference between using the new
keyword versus using the create
function. One or two Ember objects specifically mention using the new
keyword in their documentation (Set for example). Yet most Ember documentation uses the create
method, and some people even mention that using new
is just plain wrong.
So, is there any real difference between using new
to instantiate an Ember object and using the create
method?
1 Like
I almost forgot I posted this. Anyway, I looked at the source code, and here’s the relevant snippet:
create: function() {
var C = this;
if (arguments.length>0) { this._initProperties(arguments); }
return new C();
}
So it looks like the only difference is that create
will initialize properties (if you pass some in) and new
won’t. So if you don’t need to initialize any properties at creation, or you need to pass the argument directly to init
, it looks like using new
is just fine.
It’s been a while, but I wanted to give an update to this. Today, we experienced a bug caused by using new Ember.Object()
instead of Ember.Object.create()
. In other words, I wouldn’t ever recommend using the new
keyword with an Ember object subclass.