课程表

CoffeeScript 语法

CoffeeScript 类和对象

CoffeeScript 字符串

CoffeeScript 数组

CoffeeScript 日期和时间

CoffeeScript 数学

CoffeeScript 方法

CoffeeScript 元编程

CoffeeScript jQuery

CoffeeScript 正则表达式

CoffeeScript 网络

CoffeeScript 设计模式

CoffeeScript 数据库

CoffeeScript 测试

工具箱
速查手册

CoffeeScript 解构赋值

当前位置:免费教程 » JS/JS库/框架 » CoffeeScript

CoffeeScript 实现 ECMAScript Harmony 的提议 解构赋值 语法, 这样从复杂的数组和对象展开数据会更方便一些. 当你把数组或者对象的字面量赋值到一个变量时, CoffeeScript 把等式两边都解开配对, 把右边的值赋值给左边的变量. 最简单的例子, 可以用来并行赋值:

CoffeeScript:
  1. theBait = 1000
  2. theSwitch = 0
  3.  
  4. [theBait, theSwitch] = [theSwitch, theBait]
编译成JS:
  1. var theBait, theSwitch, _ref;
  2.  
  3. theBait = 1000;
  4.  
  5. theSwitch = 0;
  6.  
  7. _ref = [theSwitch, theBait], theBait = _ref[0], theSwitch = _ref[1];

用来处理函数多返回值也很方便。

CoffeeScript:
  1. weatherReport = (location) ->
  2. # 发起一个 Ajax 请求获取天气...
  3. [location, 72, "Mostly Sunny"]
  4.  
  5. [city, temp, forecast] = weatherReport "Berkeley, CA"
编译成JS:
  1. var city, forecast, temp, weatherReport, _ref;
  2.  
  3. weatherReport = function(location) {
  4. return [location, 72, "Mostly Sunny"];
  5. };
  6.  
  7. _ref = weatherReport("Berkeley, CA"), city = _ref[0], temp = _ref[1], forecast = _ref[2];

解构赋值可以用在深度嵌套的数组跟对象上, 取出深度嵌套的属性。

CoffeeScript:
  1. futurists =
  2. sculptor: "Umberto Boccioni"
  3. painter: "Vladimir Burliuk"
  4. poet:
  5. name: "F.T. Marinetti"
  6. address: [
  7. "Via Roma 42R"
  8. "Bellagio, Italy 22021"
  9. ]
  10.  
  11. {poet: {name, address: [street, city]}} = futurists
编译成JS:
  1. var city, futurists, name, street, _ref, _ref1;
  2.  
  3. futurists = {
  4. sculptor: "Umberto Boccioni",
  5. painter: "Vladimir Burliuk",
  6. poet: {
  7. name: "F.T. Marinetti",
  8. address: ["Via Roma 42R", "Bellagio, Italy 22021"]
  9. }
  10. };
  11.  
  12. _ref = futurists.poet, name = _ref.name, (_ref1 = _ref.address, street = _ref1[0], city = _ref1[1]);

解构赋值还可以跟 splats 搭配使用。

CoffeeScript:
  1. tag = ""
  2. [open, contents..., close] = tag.split("")
编译成JS:
  1. var close, contents, open, tag, _i, _ref,
  2. __slice = [].slice;
  3.  
  4. tag = "";
  5. _ref = tag.split(""), open = _ref[0], contents = 3 <= _ref.length ? __slice.call(_ref, 1, _i = _ref.length - 1) : (_i = 1, []), close = _ref[_i++];

展开式(expansion)可以用于获取数组结尾的元素, 而不需要对中间过程的数据进行赋值. 它也可以用在函数参数的列表上。

CoffeeScript:
  1. text = "Every literary critic believes he will
  2. outwit history and have the last word"
  3.  
  4. [first, ..., last] = text.split " "
编译成JS:
  1. var first, last, text, _ref;
  2.  
  3. text = "Every literary critic believes he will outwit history and have the last word";
  4.  
  5. _ref = text.split(" "), first = _ref[0], last = _ref[_ref.length - 1];

解构赋值也可以用在 class 的构造器上, 从构造器配置对象赋值到示例属性上。

CoffeeScript:
  1. class Person
  2. constructor: (options) ->
  3. {@name, @age, @height} = options
  4.  
  5. tim = new Person age: 4
编译成JS:
  1. var Person, tim;
  2.  
  3. Person = (function() {
  4. function Person(options) {
  5. this.name = options.name, this.age = options.age, this.height = options.height;
  6. }
  7.  
  8. return Person;
  9.  
  10. })();
  11.  
  12. tim = new Person({
  13. age: 4
  14. });
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号