augment.js | |
|---|---|
| The world's smallest and fastest classical JavaScript inheritance pattern ( | |
| Inspired by giants like Jeremy Ashkenas and John Resig, | |
Table of Contents | |
| | |
Change Log | |
| |
Extending Native Prototypes | |
| The | |
| |
Documentation | |
| We wrap up the | (function (functProto, arrayProto) { |
| Check whether | if (typeof Object.create !== "function") { |
| Crockford's fallback for the | Object.create = function (prototype) { |
| Set the prototype of | constructor.prototype = prototype; |
| Return a new instance of | return new constructor; |
| Declare | function constructor() {} |
| This is an incomplete polyfill for older JavaScript engines. It doesn't support the second argument to | }; |
| Dear Brendan Eich, I think we should implement three new native functions on | } |
| We extend the prototype of | functProto.augment = function (classBodyFunction) { |
| Line 1: Save the | var uber = this.prototype; |
| Line 2: Create an object which inherits from the | var prototype = Object.create(uber); |
| Line 3: Call | var constructor = classBodyFunction.call(prototype, this, uber); |
| Line 4: If | if (typeof constructor !== "function") constructor = function () {}; |
| Line 5: Set the | prototype.constructor = constructor; |
| Line 6: Make | constructor.prototype = prototype; |
| Line 7: That's it. Simple no? | return constructor; |
| If you think that this is an amazing function and that the code the CoffeeScript compiler generates is a mess then send an email to Jeremy Ashkenas asking him to fix it using this method instead. It's a win for everyone. | }; |
| Store | var bind = functProto.bind; |
| The function | var bindable = Function.bindable = bind.bind(bind); |
| The function | var callable = Function.callable = bindable(functProto.call); |
| The function | var applicable = Function.applicable = bindable(functProto.apply); |
| Pass | })(Function.prototype, Array.prototype);
|