Last active
September 28, 2016 16:25
-
-
Save zerobias/996e472615c7d5614dcf3613a34323e2 to your computer and use it in GitHub Desktop.
Metaprogramming in ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols/ | |
//Symbol | |
//Giving developers ability to add hooks to their objects, through your API | |
// Retreive the magic inspect Symbol from the API's Symbol constants | |
var inspect = console.Symbols.INSPECT | |
var myVeryOwnObject = {} | |
console.log(myVeryOwnObject) // logs out `{}` | |
myVeryOwnObject[inspect] = function () { return 'DUUUDE' } | |
console.log(myVeryOwnObject) // logs out `DUUUDE` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Reflect | |
//defineProperty | |
function MyDate() { | |
/*…*/ | |
} | |
// Old Style, weird because we're using Object.defineProperty to define | |
// a property on Function (why isn't there a Function.defineProperty?) | |
Object.defineProperty(MyDate, 'now', { | |
value: () => currentms | |
}); | |
// New Style, not weird because Reflect does Reflection. | |
Reflect.defineProperty(MyDate, 'now', { | |
value: () => currentms | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Symbol | |
//Abstract Equality Operator (== for short) | |
class AnswerToLifeAndUniverseAndEverything { | |
[Symbol.toPrimitive](hint) { | |
if (hint === 'string') { | |
return 'Like, 42, man'; | |
} else if (hint === 'number') { | |
return 42; | |
} else { | |
// when pushed, most classes (except Date) | |
// default to returning a number primitive | |
return 42; | |
} | |
} | |
} | |
var answer = new AnswerToLifeAndUniverseAndEverything(); | |
+answer === 42; | |
Number(answer) === 42; | |
''+answer === 'Like, 42, man'; | |
String(answer) === 'Like, 42, man'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example from http://schier.co/post/method-chaining-in-javascript | |
// define the class | |
var Kitten = function() { | |
this.name = 'Garfield'; | |
this.color = 'brown'; | |
this.gender = 'male'; | |
}; | |
Kitten.prototype.setName = function(name) { | |
this.name = name; | |
return this; | |
}; | |
Kitten.prototype.setColor = function(color) { | |
this.color = color; | |
return this; | |
}; | |
Kitten.prototype.setGender = function(gender) { | |
this.gender = gender; | |
return this; | |
}; | |
Kitten.prototype.save = function() { | |
console.log( | |
'saving ' + this.name + ', the ' + | |
this.color + ' ' + this.gender + ' kitten...' | |
); | |
// save to database here... | |
return this; | |
}; | |
// use it | |
new Kitten() | |
.setName('Bob') | |
.setColor('black') | |
.setGender('male') | |
.save(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Proxies | |
//Fluent API | |
//https://github.com/keithamus/proxy-fluent-api | |
function urlBuilder(domain) { | |
var parts = []; | |
var proxy = new Proxy(function () { | |
var returnValue = domain + '/' + parts.join('/'); | |
parts = []; | |
return returnValue; | |
}, { | |
has: function () { | |
return true; | |
}, | |
get: function (object, prop) { | |
parts.push(prop); | |
return proxy; | |
}, | |
}); | |
return proxy; | |
} | |
var google = urlBuilder('http://google.com'); | |
assert(google.search.products.bacon.and.eggs() === 'http://google.com/search/products/bacon/and/eggs') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Symbol | |
//Symbol.iterator | |
class Collection { | |
*[Symbol.iterator]() { | |
var i = 0; | |
while(this[i] !== undefined) { | |
yield this[i]; | |
++i; | |
} | |
} | |
} | |
var myCollection = new Collection(); | |
myCollection[0] = 1; | |
myCollection[1] = 2; | |
for(var value of myCollection) { | |
console.log(value); // 1, then 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment