What's the difference between ember.js
Object methods extend
and create
?
TLDR: You will find the answer in Ember guides: classes and instances.
Sometimes I see that one in the examples and sometimes the other. In particular, what's the difference between Em.Application.extend({})
and Em.Application.create({})
?
If I declare my app like this, what does it mean?
Ember.Application.create({ MyController : Ember.ArrayController.extend({ }), });
How can I access the instance of MyController? Do I need to create it somehow? I need to push some data into it.
create (arguments) public Accepts either no arguments, or an object containing values to initialize the newly instantiated object with. import EmberObject from '@ember/object'; const Person = EmberObject. extend({ helloWorld() { alert(`Hi, my name is ${this. get('name')}`); } }); let tom = Person.
To define a new Ember class, call the extend() method on Ember. Object : App. Person = Ember.
The dead simple answer is that extend
defines a new JS Class which inherits from the class you're extending, but does not create an instance of that class. create
creates an instance of the class.
Em.Application
is a particular case, because you're creating a namespace, not an object instance. I don't know when you'd ever want to extend
Em.Application
.
App = Em.Application.create(); // I have a new Em.Application namespace App.A = Em.Object.extend(); // I have defined a new class, App.A, // which inherits from Em.Object var a = App.A.create(); // a now contains an instance of App.A.
I'd suggest you read "Naming Conventions", too.
ETA: And "Understanding Ember Objects", as suggested in zaplitny's comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With