EmberScreencasts

Object Instantiation

var musk          = Capitalist.create()

 

var Franchisee = Capitalist.extend()

 

Instantiation

Inheritance

Capitalist.reopen({

    conscious: true

})

Introspection

var musk  = Entrepreneur.create()

var page   = Entrepreneur.create()

var bezos = Entrepreneur.create({

    location: 'Seattle'

})

 

instances

class

Instances do the work

var Entrepreneur = Ember.Capitalist.extend(GritMixin, {
    foundCompany: function(name){
        this.gatherCofounders();
        while(!this.get('productMarketFit')){
            this.suffer();
            this.pivot();
        }
        this.scale();
        this.profit();
    }
});
Entrepreneur.foundCompany('uberForScreencasts') 
// undefined is not a function!
var musk = Entrepreneur.create({name: 'Elon'});
var tesla = musk.foundCompany('Tesla')
// it works!

Instances do the work

(template edition)

{{musk.name}} //=> Elon

{{Entrepreneur.name}} //=> error
var musk = Entrepreneur.create({name: 'Elon'});

Sometimes Ember instantiates for you

//javascript file
export default Ember.Controller.extend({
  isEditing: false
})

//handlebars file
{{controller.isEditing}}

Review

  • Classes can do the following three things
    • Inheritance (.extend)
    • Instantiation (.create)
    • Introspection (.reopen)
  • var instance = Class.create()
  • Instances can take action,  Classes can't
  • Sometimes Ember Instantiates for you

EmberScreencasts

On Init

Init

init: function(){
    this.haveADream();
}
init: function(){
    this._super.apply(this, arguments);
    this.haveADream();
}
haveADream: function(){
    this.goToBed();
    this.remSleep();
}.on('init')

Review

  • 'init' is called on instantiation
  • Use .on('init') so you don't stomp on other's init functionality
    • unless you need to specifically run before or after that functionality

Object Instantiation and Init

By Jeffrey Biles

Object Instantiation and Init

  • 1,008