learning from libraries

23
LEARNING FROM LIBRARIES | Trevor Landau @trevor_landau

Upload: trevor-landau

Post on 20-Jun-2015

324 views

Category:

Technology


0 download

DESCRIPTION

My Talk from http://www.meetup.com/html5-app-developers/events/135970912/

TRANSCRIPT

Page 1: Learning from libraries

LEARNING FROM LIBRARIES | Trevor Landau @trevor_landau

Page 2: Learning from libraries

ABOUT

Page 3: Learning from libraries

WHAT LIBS?

Page 4: Learning from libraries

WHAT DID I MAKE?function Collection() {}

Page 5: Learning from libraries

JQUERY

Page 6: Learning from libraries

NOCONFLICTwindow.Collection = 'foo';var oldC = window.Collection;Collection.noConflict = function () { window.Collection = oldC; return Collection;};

var C = Collection.noConflict();log(typeof C, typeof Collection); // object, string

Page 7: Learning from libraries

LENGTHCollection.prototype = { length: 0};var c = new Collection;Array.prototype.push.call(c, 1, 2);log(c.length); // 2

Page 8: Learning from libraries

CONSTRUCTOR FACADEfunction Collection(args) { if (args && !Array.isArray(args) && arguments.length >= 0) { if (typeof args.toArray === 'function') { args = args.toArray(); } else { args = __slice.apply(arguments); } } else { args = []; } if (args.length) Array.prototype.push.apply(this, args);}

var c = new Collection([1,2,3]);var c2 = new Collection(1,2,3);var c3 = new Collection({ toArray: function () { return [1,2,3]; } });log(c.length, c2.length, c3.length); // 3, 3, 3

Page 9: Learning from libraries

BACKBONE

Page 10: Learning from libraries

ALL ARRAY BELONG TO USfunction Collection(args) { this.push.apply(this, args);}var omit = ['length', 'constructor', 'toString', 'toLocalString'];Object.getOwnPropertyNames(arrProto).forEach(function (fn) { if (omit.indexOf(fn) > -1) return; Collection.prototype[fn] = function () { var args = __slice.call(arguments); return arrProto[fn].apply(this, args); };});var c = new Collection([1,2,3]);log(c.length); // 3log(c.unshift(0)); // [0, 1, 2, 3]

Page 11: Learning from libraries

ON && TRIGGERc.on('push', function () { log('push event', arguments); // 4, 5, 6});c.push(4, 5, 6);

Page 12: Learning from libraries

ONon: function (name, fn, ctx) { this._events = this._events || {}; var events = this._events[name] || (this._events[name] = []); events.push({ fn: fn, ctx: ctx || this }); return this;}

Page 13: Learning from libraries

TRIGGERtrigger: function (name) { if (!this._events) return this; var args = __slice.call(arguments, 1); var events = this._events[name]; if (events) { events.forEach(function (ev) { ev.fn.apply(ev.ctx, args); }); } return this;}

Page 14: Learning from libraries

LETS SEE IN OUR APIObject.getOwnPropertyNames(arrProto).forEach(function (fn) { if (omit.indexOf(fn) > -1) return; Collection.prototype[fn] = function () { var args = __slice.call(arguments); var ret = arrProto[fn].apply(this, args); this.trigger(fn, ret); return ret; };});

Page 15: Learning from libraries

TOJSONCollection.prototype.toJSON = function () { return this.map(function (val) { return val; });};var c = new Collection(1,2,3);// Without methodJSON.stringify(c); // '{"0":1,"1":2,"2":3}' - will also contain other props...// With methodJSON.stringify(c); // '[1,2,3]'

Page 16: Learning from libraries

UNDERSCORECollection.zip = function () { var args = __slice.call(arguments); var lengths = args.map(function (c) { return c.length; }); var length = Math.max.apply(Math, lengths); var results = new Array(length); var map = function (c) { return c[i]; }; for (var i = 0; i < length; i += 1) { results[i] = args.map(map); } return results;}var c = new Collection([1,2]);var c2 = new Collection([3,4]);console.log(Collection.zip(c, c2)); // [[1,3],[2,4]];

Page 17: Learning from libraries

ANGULARhttp://jsperf.com/apply-vs-call-vs-invoke

// performant applyvar pApply = function (args, fn, ctx) { switch (args.length) { case 0: return ctx[fn](); case 1: return ctx[fn](args[0]); case 2: return ctx[fn](args[0], args[1]); case 3: return ctx[fn](args[0], args[1], args[2]); case 4: return ctx[fn](args[0], args[1], args[2], args[3]); case 5: return ctx[fn](args[0], args[1], args[2], args[3], args[4]); ... case 10: return ctx[fn](args[0], args[1], args[2], args[3], args[4], args[5 default: return ctx[fn].apply(ctx, args); }};

Page 18: Learning from libraries

LET'S SEE IT IN THE APIfunction Collection(args) { // Accept different types args = this._toArray(args) || []; pApply(args, 'push', this);}

Page 19: Learning from libraries

MOCHACollection.prototype.repr = function () { var self = this; // override toJSON for repr purposes this.toJSON = null;

var repr = JSON.stringify(this, function (key, val) { if (self === val || !isNaN(parseInt(key, 10))) { return val; } return void 0; });

// reset delete this.toJSON; return repr;}

Page 20: Learning from libraries

SHOULD.JS

Page 21: Learning from libraries

GETCollection.prototype = { get size() { return this.length; }, get json() { return JSON.stringify(this); }};

Page 22: Learning from libraries

DEFINEPROPERTYvar omit = ['length', 'constructor', 'toString', 'toLocalString'];Object.getOwnPropertyNames(arrProto).forEach(function (fn) { if (omit.indexOf(fn) > -1) return; Object.defineProperty(Collection.prototype, fn, { value: function () { var args = __slice.call(arguments); var ret = arrProto[fn].apply(this, args); var evArgs = [fn, ret]; pApply(evArgs, 'trigger', this); return ret; } });});

Page 23: Learning from libraries

THE ENDhttps://github.com/landau/learningfromlibraries