CoffeeScript对`undefined`的赋值

时间:2014-05-26 14:56:11

标签: javascript coffeescript undefined

查看CoffeeScript Ristretto中的Queue代码:

class Queue
  constructor: ->
    @array = []
    @head = 0
    @tail = -1
  pullHead: ->
    unless @isEmpty()
      do (value = @array[@head]) =>
        @array[@head] = undefined
        @head += 1
        value

为什么CoffeeScript会在JavaScript中将= undefined编译为void 0

Queue.prototype.pullHead = function() {
  if (!this.isEmpty()) {
    return (function(_this) {
      return function(value) {
        _this.array[_this.head] = void 0; // <------ not undefined, but `void 0`?
        _this.head += 1;
        return value;
      };
    })(this)(this.array[this.head]);
  }
};

1 个答案:

答案 0 :(得分:1)

void运算符的值始终 undefined。我不确定为什么Coffeescript会将undefined翻译成undefined,除非它可能会缩短几个字符,并且不会遇到{{1}}仅仅是符号而不是保留字的问题