Skip to content

Instantly share code, notes, and snippets.

@zerobias
Last active September 28, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerobias/996e472615c7d5614dcf3613a34323e2 to your computer and use it in GitHub Desktop.
Save zerobias/996e472615c7d5614dcf3613a34323e2 to your computer and use it in GitHub Desktop.
Metaprogramming in ES6
//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`
//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
});
//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';
// 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();
//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')
//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