Here's the standard example:
var x = "bar";
(function() {
console.log(x) // undefined
var x = 42;
})()
But what happens when you have a parameter?
/* | |
* grunt | |
* https://github.com/cowboy/grunt | |
* | |
* Copyright (c) 2012 "Cowboy" Ben Alman | |
* Copyright (c) 2012 John K. Paul @johnkpaul | |
* Licensed under the MIT license. | |
* http://benalman.com/about/license/ | |
*/ |
Here's the standard example:
var x = "bar";
(function() {
console.log(x) // undefined
var x = 42;
})()
But what happens when you have a parameter?
macro conspair { | |
case [$head] => { [$head] } | |
case [$head $tail ...] => { | |
[$head, conspair [$tail ...]] | |
} | |
} | |
conspair [1 2 3 4] |
macro def { | |
case $name:ident ( $($params:ident : $type:ident) (,) ...) $body => { | |
// just throwing away the type annotation. The semantics of type | |
// annotations left as an exercise to the reader :) | |
function $name ($params (,) ...) $body | |
} | |
} | |
def add (a : Number, b : Number) { | |
return a + b; |
macro null_helper { | |
case ($processed ...) ($rest) => { | |
$processed (.) ... . $rest | |
} | |
case ($processed ...) ($rest_head $rest ...) => { | |
$processed (.) ... . $rest_head | |
&& null_helper ($processed ... $rest_head) ($rest ...) | |
} | |
} |
macro sexp { | |
case () => { | |
; | |
} | |
case ($p) => { | |
$p | |
} | |
case ($x $y) => { | |
$x($y); | |
} |
macro class { | |
case $className { constructor $constParam $constBody $rest ... } => { | |
function $className $constParam $constBody | |
class $className { $rest ... } | |
} | |
case $className { private_function $pMethodName $pMethodParam $pMethodBody $rest ...} => { | |
function $pMethodName $pMethodParam $pMethodBody | |
class $className { $rest ... } |
macro _match_cond { | |
case $o ($field) => { | |
(typeof $o.$field !== 'undefined') | |
} | |
case $o ($field $rest ...) => { | |
_match_cond $o ($field) && _match_cond $o ($rest ...) | |
} | |
} | |
macro _match_var { |
macro _arms { | |
case (default => $value:expr) => { | |
else { | |
return $value; | |
} | |
} | |
case (case $cond:expr => $value:expr) => { | |
if($cond) { | |
return $value; | |
} |
macro forThing { | |
case $val ($a:expr, $b:expr) => {(function($val){forEach.call($b, this)}).bind($a)} | |
case $val ($a:expr) => {(function($val){forEach.call(val,this)}).bind($a)} | |
} | |
function makeMutationObserver(tag){ | |
var queue= tag.queue, | |
put= queue.put.bind(queue), | |
mutationAddObserve= forThing val (put, val.getElementsByTagName(this.tag)), | |
mutationObserver= forThing val (mutationAddObserve, val.addedNodes), | |
mutationsObserver= forThing val (mutationObserver) |