zoukankan      html  css  js  c++  java
  • coffeescript初体验

    第一时间就被吸引了,个人觉得值得一用,简练、实用,熟练的话写代码本身都变得美好了,而且看上去很高大上有木有

    安装

    npm install coffee-script -g

    npm是Nodejs带的一个包管理工具,下载安装好nodejs就可以用npm了。-g是指把包安装到全局路径下,这样无论当前路径在哪你都可以使用它

    顺带提一下如何更改全局路径

    npm root -g  //查看全局路径
    npm config set prefix path/to/global_modules  //设置路径前缀,npm会在该目录下自动创建node_modules目录存放各种包
    npm config set cache path/to/global_cache

    最后将你的path/to/global_modules路径添加到系统的环境变量里,就搞定了

    使用

     现在可以在任意位置下使用coffee命令了

    最常用的就是把.coffee文件编译成.js文件了

    coffee -o path/to/js/ -c path/to/coffee/  //将coffee/下的所有.coffee文件编译为同名.js文件放到js/下

    还有其他很实用的命令比如监听等,详见官网tutorial

    使用sublime的玩家推荐用better coffeescript插件

    用coffee来写以前的代码

    set = (name, value, options = {}) ->
            if value is null
                options.maxAge = 0
                options.expires = -1
            if value and typeof value is 'object'
                value = JSON.stringify(value)
            str = encodeURIComponent(name) + '=' + encodeURIComponent(value)
            if options.expires?
                d = new Date()
                d.setTime(d.getTime() + options.expires * 1000)
                str += '; expires=' + d.toUTCString()
            if options.maxAge? then str += '; max-age=' + options.maxAge
            if options.path? then str += '; path=' + options.path
            if options.domain? then str += '; domain=' + options.domain
            if options.secure? then str += '; secure'
            document.cookie = str

    对于coffeescript我个人觉得需要吐槽的一点是官方代码里把调用函数时参数外的()都省略了,这样可读性明显变差了,写的时候还是带上括号舒服的多

  • 相关阅读:
    洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告
    洛谷 P1640 [SCOI2010]连续攻击游戏 解题报告
    vector-pop_back
    vector-push_back
    vector-push_back
    vector-max_size
    vector-max_size
    vector-insert
    vector-insert
    vector-front
  • 原文地址:https://www.cnblogs.com/coiorz/p/5167612.html
Copyright © 2011-2022 走看看