Difference between set and this.set

I have seen people writing code using both set import from ‘@ember/object’ and this.set(key, value). I don’t understand the difference between those two. Can somebody please help me understand the difference?

1 Like

Your best starting point for questions like this is the API docs! You can search for set and it will take you to theses two definitions:

The net is that you can use the imported function set with any object in JS, whether it’s related to Ember in any way or not. this.set is a method that only exists on EmberObject and its subclasses, so you can only use it when you’re working in one of those subclasses (like Route, Controller, Service, etc.).

Note:

  • You therefore cannot use this.set in a Glimmer component: it is not a subclass of EmberObject.
  • But you also wouldn’t normally need to, because in Ember Octane, you generally would be using auto-tracking with the @tracked decorator, and therefore could just use normal JS assignment without worrying about set at all!
1 Like