用 CoffeeScript 扩展 Javascript 对象 [英] Extending Javascript objects with CoffeeScript

查看:17
本文介绍了用 CoffeeScript 扩展 Javascript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过向原型添加方法来添加扩展 javascript 对象的能力.
该方法将接收一个或多个其他对象并将所有键/值添加到 this.
这是我想出的:

I want to add the ability to extend javascript objects by adding a method to the prototype.
The method will receive one or more other objects and will add all of the key/values to this.
This is what I came up with:

Object::extend = (objects...) ->
    @[key] = value for key, value of object for object in objects

或者这个:

Object::extend = (objects...) ->
    for object in objects
        for key, value of object
            @[key] = value 

两者都按预期工作,并编译成相同的 javascript 代码:

Both work as expected, and compile into the same javascript code:

var __slice = [].slice;

Object.prototype.extend = function() {
  var key, object, objects, value, _i, _len, _results;
  objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  _results = [];
  for (_i = 0, _len = objects.length; _i < _len; _i++) {
    object = objects[_i];
    _results.push((function() {
      var _results1;
      _results1 = [];
      for (key in object) {
        value = object[key];
        _results1.push(this[key] = value);
      }
      return _results1;
    }).call(this));
  }
  return _results;
};

我不太满意的是每个 for 循环创建的整个结果,这对于我的目的来说是完全多余的.
有没有办法让代码更像:

What I'm not too happy about is the whole results thing that is created per for loop which is completely redundant for my purpose.
Is there a way to get a code more like:

Object.prototype.extend = function() {
  var key, object, objects, value, _i, _len;
  objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  for (_i = 0, _len = objects.length; _i < _len; _i++) {
    object = objects[_i];
    (function() {
      for (key in object) {
        value = object[key];
        this[key] = value;
      }
    }).call(this);
  }
};

谢谢.

我知道我可以简单地嵌入 javascript 代码,但要寻找咖啡脚本解决方案.

I'm aware that I can simply embed javascript code, but looking for a coffeescript solution.

推荐答案

你可以尝试添加一个显式的return:

You can try adding an explicit return:

Object::extend = (objects...) ->
    for object in objects
        for key, value of object
            @[key] = value
    return 

产生这个:

var __slice = [].slice;

Object.prototype.extend = function() {
  var key, object, objects, value, _i, _len;
  objects = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  for (_i = 0, _len = objects.length; _i < _len; _i++) {
    object = objects[_i];
    for (key in object) {
      value = object[key];
      this[key] = value;
    }
  }
};

每个 CoffeeScript 函数都返回函数中最后一个表达式的值,CoffeeScript 循环也是表达式.这意味着 CoffeeScript 必须构建所有 _results 内容来为您的函数生成返回值,因为您有一个隐式 return 应用于外部循环.如果您通过添加显式不返回任何内容"来删除隐式 return,那么 CS→JS 编译器似乎足够聪明,不会执行所有额外的 _results 工作.

Every CoffeeScript function returns the value of the last expression in the function, CoffeeScript loops are also expressions. That means that CoffeeScript has to build all that _results stuff to produce a return value for your function since you had an implicit return applying to the outer loop. If you remove that implicit return by adding an explicit "return nothing" then the CS→JS compiler seems to be smart enough to not do all that extra _results work.

这篇关于用 CoffeeScript 扩展 Javascript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆