home
  • Blog
1.10
  • Getting Started
  • Getting Ember
  • Concepts
  • The Object Model
    • Classes and Instances
    • Computed Properties
    • Computed Properties and Aggregate Data with @each
    • Observers
    • Bindings
    • Reopening Classes and Instances
    • Bindings, Observers, Computed Properties: What Do I Use When?
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
  • Views
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.10.0.
Go to v5.0.0

Reopening Classes and Instances

Edit pencil

You don't need to define a class all at once. You can reopen a class and define new properties using the reopen method.

Person.reopen({
  isPerson: true
});

Person.create().get('isPerson') // true

When using reopen, you can also override existing methods and call this._super.

Person.reopen({
  // override `say` to add an ! at the end
  say: function(thing) {
    this._super(thing + "!");
  }
});

reopen is used to add instance methods and properties that are shared across all instances of a class. It does not add methods and properties to a particular instance of a class as in vanilla JavaScript (without using prototype).

But when you need to create class methods or add properties to the class itself you can use reopenClass.

Person.reopenClass({
  createMan: function() {
    return Person.create({isMan: true})
  }
});

Person.createMan().get('isMan') // true
left arrow
Bindings
Bindings, Observers, Computed Properties: What Do I Use When?
right arrow
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue