example.js 662 B

123456789101112131415161718192021222324252627
  1. var genfun = require('./')
  2. var multiply = function(a, b) {
  3. return a * b
  4. }
  5. var addAndMultiplyNumber = function(val) {
  6. var fn = genfun()
  7. ('function(n) {')
  8. ('if (typeof n !== "number") {') // ending a line with { will indent the source
  9. ('throw new Error("argument should be a number")')
  10. ('}')
  11. ('var result = multiply(%d, n+%d)', val, val)
  12. ('return result')
  13. ('}')
  14. // use fn.toString() if you want to see the generated source
  15. return fn.toFunction({
  16. multiply: multiply
  17. })
  18. }
  19. var addAndMultiply2 = addAndMultiplyNumber(2)
  20. console.log(addAndMultiply2.toString())
  21. console.log('(3 + 2) * 2 =', addAndMultiply2(3))