Functional styles in Ember source with `set`


#1

This is more of a curiosity. In the source I often see:

var set = Ember.set;
// ...
set(this, 'someProp', someValue);

Is this a developer preference? Why not simply call:

this.set('someProp', someValue);

And what does this have to do with es6 set?


#2

One advantage is that Ember.set works with non-Ember Objects. So, it’s safer if you’re unsure if you’re using a POJO or an Ember Object. (Also, I don’t think it has anything to do with the es6 set).

For example:

var set = Ember.set;
var obj = {};

obj.set('name', 'Spencer'); // Fails since 'obj' is not an Ember Object.
set(obj, 'name', 'Spencer'); // Works

#3

Ian!!

I don’t spend much time in the source, but from poking around the API docs I might have a guess. Ember.set (object, key, value) is defined directly in ember-metal, and the this.set(key, value) seems to come from the observable mixin. My hunch is that the former is more performant.


#4

Thanks @Spencer_Price and @kylecoberly, very useful info!