Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active October 11, 2015 16:44
Show Gist options
  • Save wilmoore/72971fc5349fe22ed303 to your computer and use it in GitHub Desktop.
Save wilmoore/72971fc5349fe22ed303 to your computer and use it in GitHub Desktop.
The new JS class syntax is no panacea as it is implied in the video. I mean, there are other ways to fix this: https://youtu.be/05Z6YGiZKmE?t=11m42s
'use strict'
/*!
* exports.
*/
module.exports = initialize
/*!
* object initialization.
*/
function initialize () {
return Object.assign({ files: { minified: [] } })
}
'use strict'
/*!
* imports.
*/
const infile = require('./infile')
/*!
* exports.
*/
module.exports = initialize
/*!
* object initialization.
*/
function initialize () {
return Object.assign(infile(), { addJavaScript })
}
/*!
* Add JavaScript file.
*/
function addJavaScript (data, sourceMap, path) {
this.files.minified.push({ data, sourceMap, path })
}
@wilmoore
Copy link
Author

wilmoore commented Oct 7, 2015

You may prefer it terse:

module.exports = function initialize () {
  return Object.assign(infile(), {
    addJavaScript (data, sourceMap, path) {
      this.files.minified.push({ data, sourceMap, path })
    }
  })
}

You may also like ES6 class:

export class JsFile extends InputFile {
  addJavaScript({ data, sourceMap, path }) {
    this._minifiedFiles.push({ data, sourceMap, path })
  }
}

Just keep in mind, ES6 class is not necessary the correct tool for everything.

@sylvainpolletvillard
Copy link

If I want to follow the "composition over inheritance" principle, should I do this ?

var compose = Object.assign.bind(Object, {})

module.exports = compose(InputFile, {
  addJavaScript({ data, sourceMap, path }) {
    this._minifiedFiles.push({ data, sourceMap, path })
  }
})

I don't think Meteor can handle this

@wilmoore
Copy link
Author

@sylvainpolletvillard

Looks good to me. The only thing to be aware of is if you are supporting older browsers (IE 8/9) you may want to pull in a replacement for Object.assign and Object.create since those have spotty or no support for ES5 features. I don't like using big shim libraries but rather like to replace just the functions I know are problematic.

@wilmoore
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment