Defines new protocol that may be implemented for different types. ## Example var sequence = protocol.define(('Logical list abstraction', { first: ('Returns the first item in the sequence', [ protocol ]), rest: ('Returns a sequence of the items after the first', [ protocol ]), stick
(type, methods)
| 65 | |
| 66 | **/ |
| 67 | function Protocol(type, methods) { |
| 68 | /** |
| 69 | Extends this protocol by implementing it for the given `type`. |
| 70 | |
| 71 | ## Example |
| 72 | |
| 73 | sequence(String, { |
| 74 | first: function(string) { return string[0] || null }, |
| 75 | rest: function(string) { return String.prototype.substr.call(string, 1) }, |
| 76 | stick: function(item, string) { return item + string } |
| 77 | }) |
| 78 | sequence(Array, { |
| 79 | first: function(array) { return array[0] || null }, |
| 80 | rest: function(array) { return Array.prototype.slice.call(array, 1) }, |
| 81 | stick: function(item, array) { |
| 82 | return Array.prototype.concat.call([ item ], array) |
| 83 | } |
| 84 | }) |
| 85 | **/ |
| 86 | var types = slice(arguments) |
| 87 | methods = types.pop() |
| 88 | if (!types.length) extend(Protocol, Default, methods) |
| 89 | else while (types.length) extend(Protocol, types.shift(), methods) |
| 90 | return type |
| 91 | } |
| 92 | |
| 93 | Protocol.signature = signature |
| 94 | var descriptor = {} |