module.exports = classImageJpgextendsImage { constructor(path) { if (!path.match(/\.jpe?g$/)) { thrownewError(`${path} is not a JPEG image`); } super(path); } };
imagePng.js
1 2 3 4 5 6 7 8 9 10 11 12
"use strict";
constImage = require('./image');
module.exports = classImagePngextendsImage { constructor(path) { if (!path.match(/\.png$/)) { thrownewError(`${path} is not a PNG image`); } super(path); } };
imageGif.js
1 2 3 4 5 6 7 8 9 10 11 12
"use strict";
constImage = require('./image');
module.exports = classImageGifextendsImage { constructor(path) { if (!path.match(/\.gif/)) { thrownewError(`${path} is not a GIF image`); } super(path); } };
functioncreatePerson(name) { const privateProperties = {}; const person = { setName: name => { if (!name) thrownewError('A person must have a name'); privateProperties.name = name; }, getName: () => privateProperties.name };
person.setName(name); return person; }
const person = createPerson('Tomas Lin'); console.log(person.getName(), person);
module.exports = function (label) { if (process.env.NODE_ENV === 'development') { returnnewProfiler(label); //[1] } elseif (process.env.NODE_ENV === 'production') { return { //[2] start: function () { }, end: function () { } } } else { thrownewError('Must set NODE_ENV'); } }
profilerTest.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const profiler = require('./profile');
functiongetRandomArray(len) { const p = profiler(`Generating a ${len} items long array`); p.start(); const arr = []; for (let i = 0; i < len; i++) { arr.push(Math.random()); } p.end(); }