0

I'm reading through this Flux tutorial and the objects are created there like this:

var AppDispatcher = assign({}, Dispatcher.prototype, {

  /**
   * A bridge function between the views and the dispatcher, marking the action
   * as a view action.  Another variant here could be handleServerAction.
   * @param  {object} action The data coming from the view.
   */
  handleViewAction: function(action) {
    this.dispatch({
      source: 'VIEW_ACTION',
      action: action
    });
  }

});

They use Object.assign through polyfill. Since Object.assign copies all properties to the object as opposed to Object.create(prototype), I'm wondering what their motivation is to copy the properties directly onto AppDispatcher. Any ideas?

1 Answer 1

1

It's just creating a copy of Dispatcher, based on its prototype.

If you were to do AppDispatcher = Dispatcher;, and then you mutate AppDispatcher, you would also be mutating Dispatcher. assign in this case just creates a safe copy, and then lets you assign properties to that copy.

Sign up to request clarification or add additional context in comments.

7 Comments

can you elaborate on that part ...based on its prototype? And how the same can be accomplished without using assign?
@Maximus, You could do var AppDispatcher = Object.create(Dispatcher.prototype) and then set AppDispatcher.handleViewAction = /* whatever */. The way they do it is just for convenience. Makes it more concise. There's nothing fancy or hidden about it, it's just another way of doing it.
with Object.create, properties available on Dispatcher.prototype will be accessible through prototype chain, but I thought that assign copies these properties and makes them own properties of AppDispatcher, no?
@Maximus, if the polyfill assign directly mimicks Object.assign from ES6, then yes, assign will copy all of the prototype's properties directly onto the AppDispatcher. I haven't looked in the source of assign, but I assume that that's the case.
So then Object.create approach is different, right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.