Created
January 30, 2011 20:49
-
-
Save rwaldron/803236 to your computer and use it in GitHub Desktop.
Various pre-Harmony bits and pieces
This file contains 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
<script type="application/javascript;version=1.8"> | |
var toString = Object.prototype.toString; | |
console.log( | |
[ i for each ( i in [ 1,2,3,4,5 ] ) ] | |
); | |
function $r( floor, ceil ) { | |
// works with `var` | |
for( let i = floor; i < ceil; ++i ) { | |
yield i; | |
} | |
} | |
// Returns Generator | |
console.log( $r(0, 30) ); | |
console.log( toString.call( $r(0, 30) ), "[object Generator]" ); | |
console.log( | |
// initialize an array, allow only even numbers | |
[ i for each (i in $r(0, 30) ) if ( !(i % 2) ) ] | |
); | |
console.log( | |
// initialize an array, allow only even numbers, omits 0 - treated as falsy | |
[ ( !(i % 2) && i ) for each (i in $r(0, 30) ) ].filter( function (val) { | |
return val; | |
}) | |
); | |
var alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
console.log( | |
[ a for each ( a in alphabet ) ] | |
); | |
var obj = { | |
a: "alpha", | |
b: "beta", | |
d: "delta" | |
}; | |
console.log( | |
[ p for each ( p in obj ) ] | |
); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
function Constructor( options ) { | |
var [ a, b ] = options; | |
this.a = a; | |
this.b = b; | |
} | |
var ctor = new Constructor( [ "Alpha", "Beta" ] ); | |
console.log(ctor); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
var {log, sqrt, sin} = Math; | |
console.log( "log", log( 10 ) ); | |
console.log( "sqrt", sqrt( 10 ) ); | |
console.log( "sin", sin( 10 ) ); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
function Constructor( options ) { | |
var { a, b } = options; | |
this.a = a; | |
this.b = b; | |
return this; | |
} | |
var ctor = new Constructor( { a: "Alpha", b: "Beta" } ); | |
console.log(ctor); | |
// Destructured, almost named parameters | |
// Example contributed by Alex Sexton http://slexaxton.com | |
let area = function({ width: w, height: h }) { | |
return h * w; | |
}; | |
console.log( | |
area( { width: 10, height: 15 } ) | |
); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
var a = "alpha", | |
b = "beta", | |
d = "delta", | |
g = "gamma"; | |
console.log( a, b, d, g ); | |
// destructuring array to "swap" assignments | |
[ g, d, b, a ] = [ a, b, d, g ]; | |
console.log( a, b, d, g ); | |
// | |
var arr = [ a, b, d, g ], | |
rev = arr.reverse(); | |
console.log( arr, rev ); | |
var obj = { | |
a: "alpha", | |
b: "beta", | |
d: "delta" | |
}, | |
// destructure the `a` & `b` properties | |
{ a, d } = obj | |
; | |
console.log( a, "alpha" ); | |
console.log( d, "delta" ); | |
document.addEventListener("DOMContentLoaded", function() { | |
var r = "<span style='color:red;'>X</span>", | |
o = "<span style='color:orange;'>X</span>", | |
y = "<span style='color:yellow;'>X</span>", | |
g = "<span style='color:green;'>X</span>", | |
b = "<span style='color:blue;'>X</span>", | |
i = "<span style='color:indigo;'>X</span>", | |
v = "<span style='color:violet;'>X</span>"; | |
document.body.innerHTML = [ r, o, y, g, b, i, v ].join("") + "<br>"; | |
for ( var x = 0; x < 49; x++ ) { | |
// Each iteration will re-assign | |
// current first = current last position | |
[ r, o, y, g, b, i, v ] = [ o, y, g, b, i, v, r ]; | |
// Output current state to page | |
document.body.innerHTML += [ r, o, y, g, b, i, v ].join("") + "<br>"; | |
} | |
}, false); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
var exp = { | |
"+": function(x, y) x + y, | |
"-": function(x, y) x - y, | |
"*": function(x, y) x * y, | |
"/": function(x, y) x / y, | |
opThis: function( x, o, y ) this[o]( x, y ), | |
opArg: function( x, o, y ) new Function("return " + x + o + y )(), | |
opStr: function( oper ) new Function("return " + oper )(), | |
opExp: function( exp ) exp | |
}; | |
console.log( | |
exp["+"]( 10, 5 ), | |
exp["-"]( 10, 5 ), | |
exp["*"]( 10, 5 ), | |
exp["/"]( 10, 5 ), | |
exp.opThis( 10, "*", 5 ), | |
exp.opArg( 10, "*", 5 ), | |
exp.opStr( "10 * 5" ), | |
exp.opExp( 10 * 5 ) | |
); | |
// 15 5 50 2 50 50 50 50 | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
// Generator: returns an | |
function rangeGenerator( floor, ceil ) { | |
for ( let i = floor; i < ceil; ++i ) { | |
yield i; | |
} | |
} | |
// Create an array squares, then filter for even numbers | |
var evenSqrs = [ | |
// | |
( i * i % 2 === 0 && i ) for each (i in rangeGenerator(0, 10) ) | |
].filter( function ( val ) { | |
return val; | |
}); | |
console.log(evenSqrs); | |
</script> |
This file contains 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
<!-- CAUTION: __iterator__ has been rejected and will not proceed to ES.next standardization --> | |
<script type="application/javascript;version=1.8"> | |
var quux = { | |
a: "alpha", | |
b: "beta", | |
d: "delta" | |
}, | |
quuxIter = Iterator( quux ); | |
for ( var keyVal in quuxIter ) { | |
console.log( keyVal ); | |
} | |
function Range( floor, ceil ) { | |
this.floor = floor; | |
this.ceil = ceil; | |
} | |
Range.prototype.__iterator__ = function() { | |
for( let i = this.floor; i < this.ceil; ++i ) { | |
yield i; | |
} | |
}; | |
function $R( floor, ceil ) { | |
return new Range( floor, ceil ); | |
} | |
console.log( $R( 0, 10) ); | |
console.log( | |
[ i for each ( i in new Range(0,10) ) ] | |
); | |
console.log( | |
[ i for each ( i in $R(0, 10) ) ] | |
); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
// `let` provides explicit lexical blockscope | |
// `let` definitions | |
var foo = ""; | |
console.log( "expressions", foo, "(empty)" ); | |
for ( var i = 0; i < 5; i++ ) { | |
let foo = "letting i = " + i; | |
console.log( "expressions", foo ); | |
} | |
// `foo` will still be empty | |
console.log( "expressions", foo, "(empty)" ); | |
// `let` statements | |
var foo = "Hello"; | |
console.log( "statements", foo, "Hello" ); | |
// `let` "allows" `foo` to have a new value, | |
// that is only applied within the | |
// statement's scope block | |
let ( foo = "Hola" ) { | |
console.log( "statements", foo, "Hola" ); | |
} | |
console.log( "statements", foo, "Hello" ); | |
// `let` expressions | |
var foo = "Hello"; | |
console.log( "expressions", foo, "Hello" ); | |
// `let` "allows" `foo` to have a new value, | |
// that is only applied within the expression | |
let ( foo = "Hola" ) console.log( "expressions", foo, "Hola" ); | |
console.log( "expressions", foo, "Hello" ); | |
// `let` as a for-loop initializer | |
// `var` will declare and assign `i` ... | |
for ( var i = 0; i < 10; ++i ) { | |
} | |
// `i` value is 10 | |
console.log( i, 10 ); | |
// `let` will declare and assign `x`; | |
// `x` is only available within the for-loop | |
for ( let x = 0; x < 10; x++ ) { | |
} | |
// `x` does not exist outside of the for-loop scope | |
console.log( typeof x, "undefined" ); | |
// `let` practical application test | |
function practicalLet() { | |
let x = "good morning"; | |
// let-statement | |
let ( x = x + " HAL" ) { | |
console.log( x ); | |
} | |
// let-expression | |
let ( x = x + " Dave" ) console.log( x ); | |
// let-definition | |
{ | |
let x = "open the pod bay doors HAL"; | |
console.log( x ); | |
} | |
} | |
practicalLet(); | |
</script> |
This file contains 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
<script type="application/javascript;version=1.8"> | |
var quux = { | |
alpha: "Alpha", | |
beta: "Beta", | |
delta: "Delta" | |
}; | |
// Will return an Iterator | |
function y( obj ) { | |
for ( var prop in obj ) { | |
yield prop; | |
} | |
} | |
console.log( | |
// show the first prop accessible by the iterator | |
y( quux ).next() // alpha | |
); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment