×
CoffeeScript 关于

CoffeeScript 语法

服务端和客户端的代码重用CoffeeScript 比较范围CoffeeScript 嵌入 JavaScriptCoffeeScript For 循环

CoffeeScript 类和对象

CoffeeScript 对象的链式调用CoffeeScript 类方法和实例方法CoffeeScript 类变量和实例变量CoffeeScript 克隆对象CoffeeScript 类的混合CoffeeScript 创建对象字面值CoffeeScript type 函数

CoffeeScript 字符串

CoffeeScript 大写单词首字母CoffeeScript 查找子字符串CoffeeScript 生成唯一 IDCoffeeScript 字符串插值CoffeeScript 字符串小写转换CoffeeScript 匹配字符串CoffeeScript 重复字符串CoffeeScript 拆分字符串CoffeeScript 清理字符串前后CoffeeScript 字符串大写转换

CoffeeScript 数组

CoffeeScript 检查变量是否数组CoffeeScript 将数组连接CoffeeScript 数组创建对象词典CoffeeScript 数组创建字符串CoffeeScript 定义数组范围CoffeeScript 筛选数组CoffeeScript 列表推导CoffeeScript 映射数组CoffeeScript 数组最大值CoffeeScript 归纳数组CoffeeScript 删除数组相同元素CoffeeScript 反转数组CoffeeScript 打乱数组中的元素CoffeeScript 检测每个元素CoffeeScript 用数组来交换变量CoffeeScript 对象数组类似 Python 的 zip 函数

CoffeeScript 日期和时间

CoffeeScript 计算复活节的日期CoffeeScript 计算感恩节日期计算两个日期中间的天数CoffeeScript 算1个月中最后1天CoffeeScript 找到上/下一个月CoffeeScript 计算月球的相位

CoffeeScript 数学

CoffeeScript 数学常数更快的 Fibonacci 算法平方根倒数快速算法生成可预测的随机数CoffeeScript 生成随机数CoffeeScript 转换弧度和度CoffeeScript 一个随机整数函数CoffeeScript 指数对数运算CoffeeScript 方法CoffeeScript 去抖动函数CoffeeScript 当函数括号不可选CoffeeScript 递归函数CoffeeScript 提示参数CoffeeScript 元编程检测与构建丢失函数CoffeeScript 扩展内置对象

CoffeeScript jQuery

CoffeeScript AJAXCoffeeScript 回调绑定CoffeeScript 创建 jQuery 插件CoffeeScript AJAXCoffeeScript 无jQuery的Ajax

CoffeeScript 正则表达式

CoffeeScript 使用 HeregexesHTML实体替换 HTML 标签CoffeeScript 替换子字符串CoffeeScript 查找子字符串CoffeeScript 网络CoffeeScript 客户端CoffeeScript HTTP 客户端CoffeeScript 基本HTTP服务器CoffeeScript 服务器CoffeeScript 双向客户端CoffeeScript 双向服务器

CoffeeScript 设计模式

CoffeeScript 适配器模式CoffeeScript 桥接模式CoffeeScript 生成器模式CoffeeScript 命令模式CoffeeScript 修饰模式CoffeeScript 工厂方法模式CoffeeScript 解释器模式CoffeeScript 备忘录模式CoffeeScript 观察者模式CoffeeScript 单件模式CoffeeScript 策略模式CoffeeScript 模板方法模式

CoffeeScript 数据库

CoffeeScript MongoDBCoffeeScript SQLite

CoffeeScript 测试

使用 Jasmine 测试使用 Nodeunit 测试

CoffeeScript 解释器模式


问题

其他人需要以控制方式运行你的一部分代码。相对地,你选择的语言不能以一种简洁的方式表达问题域。

解决方案

使用解释器模式来创建一个你翻译为特定代码的领域特异性语言(domain-specific language)。

我们来做个假设,例如用户希望在你的应用程序中执行数学运算。你可以让他们正向运行代码来演算指令(eval)但这会让他们运行任意代码。相反,你可以提供一个小型的“堆栈计算器(stack calculator)”语言,用来做单独分析,以便只运行数学运算,同时报告更有用的错误信息。

class StackCalculator
    parseString: (string) ->
        @stack = [ ]
        for token in string.split /s+/
            @parseToken token

        if @stack.length > 1
            throw "Not enough operators: numbers left over"
        else
            @stack[0]

    parseToken: (token, lastNumber) ->
        if isNaN parseFloat(token) # Assume that anything other than a number is an operator
            @parseOperator token
        else
            @stack.push parseFloat(token)

    parseOperator: (operator) ->
        if @stack.length < 2
            throw "Can't operate on a stack without at least 2 items"

        right = @stack.pop()
        left = @stack.pop()

        result = switch operator
            when "+" then left + right
            when "-" then left - right
            when "*" then left * right
            when "/"
                if right is 0
                    throw "Can't divide by 0"
                else
                    left / right
            else
                throw "Unrecognized operator: #{operator}"

        @stack.push result

calc = new StackCalculator

calc.parseString "5 5 +" # => { result: 10 }

calc.parseString "4.0 5.5 +" # => { result: 9.5 }

calc.parseString "5 5 + 5 5 + *" # => { result: 100 }

try
    calc.parseString "5 0 /"
catch error
    error # => "Can't divide by 0"

try
    calc.parseString "5 -"
catch error
    error # => "Can't operate on a stack without at least 2 items"

try
    calc.parseString "5 5 5 -"
catch error
    error # => "Not enough operators: numbers left over"

try
    calc.parseString "5 5 5 foo"
catch error
    error # => "Unrecognized operator: foo"

讨论

作为一种替代编写我们自己的解释器的选择,你可以将现有的CoffeeScript解释器与更自然的(更容易理解的)表达自己的算法的正常方式相结合。

class Sandwich
    constructor: (@customer, @bread='white', @toppings=[], @toasted=false)->

white = (sw) ->
    sw.bread = 'white'
    sw

wheat = (sw) ->
    sw.bread = 'wheat'
    sw

turkey = (sw) ->
    sw.toppings.push 'turkey'
    sw

ham = (sw) ->
    sw.toppings.push 'ham'
    sw

swiss = (sw) ->
    sw.toppings.push 'swiss'
    sw

mayo = (sw) ->
    sw.toppings.push 'mayo'
    sw

toasted = (sw) ->
    sw.toasted = true
    sw

sandwich = (customer) ->
    new Sandwich customer

to = (customer) ->
    customer

send = (sw) ->
    toastedState = sw.toasted and 'a toasted' or 'an untoasted'

    toppingState = ''
    if sw.toppings.length > 0
        if sw.toppings.length > 1
            toppingState = " with #{sw.toppings[0..sw.toppings.length-2].join ', '} and #{sw.toppings[sw.toppings.length-1]}"
        else
            toppingState = " with #{sw.toppings[0]}"
    "#{sw.customer} requested #{toastedState}, #{sw.bread} bread sandwich#{toppingState}"

send sandwich to 'Charlie' # => "Charlie requested an untoasted, white bread sandwich"
send turkey sandwich to 'Judy' # => "Judy requested an untoasted, white bread sandwich with turkey"
send toasted ham turkey sandwich to 'Rachel' # => "Rachel requested a toasted, white bread sandwich with turkey and ham"
send toasted turkey ham swiss sandwich to 'Matt' # => "Matt requested a toasted, white bread sandwich with swiss, ham and turkey"

这个实例可以允许功能层实现返回修改后的对象,从而外函数可以依次修改它。示例通过借用动词和介词的用法,把自然语法提供给结构,当被正确使用时,会像自然语句一样结束。这样,利用CoffeeScript语言技能和你现有的语言技能可以帮助你关于捕捉代码的问题。


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)