【问题标题】:Is there something like Ruby's module for JavaScript有没有类似 Ruby 的 JavaScript 模块
【发布时间】:2017-03-20 00:22:53
【问题描述】:

假设我有一个 Ruby 类和一个 Ruby 模块

module Foo
  def hello
    puts 'hello'
  end
end

class Bar
  include Foo
end

现在我可以做这样的事情了

Bar.new.hello 

我可以在 JavaScript 中做同样的事情吗?我不能使用 extends 关键字,因为我的 JavaScript 类已经被继承。如何在我的类中混入一些功能?

更新

我想使用与 ES6 类一起使用的东西

【问题讨论】:

  • 我想你的意思是include Foo,而不是include Bar
  • 当然。我修正了错字
  • 你尝试过 ES6 类吗?
  • @user3309314 >>我想使用一些与 ES6 类一起工作的东西stackoverflow.com/questions/30732241/…...或“es 6 类”怎么样mixins" ...stackoverflow.com/questions/38970402/es-6-classes-mixins ...第一场比赛看起来已经不仅仅是有希望了。

标签: javascript ruby module mixins es6-class


【解决方案1】:

我确实建议阅读我在 SO 上给出的一些列出的答案,这些答案与 OP 的答案非常相关。

...编辑:顺便说一句,搜索“es6 classes mixins”确实已经指向Mixins for ES6 classes, transpiled with babel ...

使用 OP 的示例,一个已经可靠的答案可能会被简单地分解为 ...

function withSayHello() {       // - function based *Flight Mixin* as of Angus Croll.
  this.sayHello = function () { // - should be encouraged becoming the main approach
                                //   (though featuring a sugared syntax) of a future
    console.log(this.hello);    //   *Lightweight Trait* implementation.
  };                            //
}                               //

function Bar() {                // - classic ES3 constructor.
    this.hello = 'hello';       //
                                //
    withSayHello.call(this);    // - applying the function based Mixin/Trait/Talent
}                               //   from above.
var bar = new Bar;

bar.sayHello();


class Foo {                     // - with syntactic "class" sugar ...
  constructor() {               //   ... `constructor` does remain the place for ...
    this.hello = 'Howdy!';      //
                                //
    withSayHello.call(this);    //   ... applying function based Mixins/Traits/Talents.
  }                             //
}                               //
var foo = new Foo;

foo.sayHello();

也可以使用Babel's REPL 检查上述示例。

浏览器仍然不支持 JavaScript 的标准化模块系统。另一方面,例如JS 库和 NodeJS 环境确实提供了自己的模块系统。 上面的工作示例很容易在每个示例中传输/转译 - 但这里再次进入标准示例,因为它已经被 BabelTraceur 等编译器支持。

// module "my_special_withSayHello_mixin.js"
//
export default function withSayHello() {
  this.sayHello = function () {

    console.log(this.hello);
  };
}


// module "my_very_own_Foo_implementation.js"
//
import withSayHello from 'my_special_withSayHello_mixin';

export default class Foo {
  constructor() {
    this.hello = 'Howdy!';

      withSayHello.call(this);
  }
}


// module "main.js"
//
import Foo from 'my_very_own_Foo_implementation';

var foo = new Foo;
foo.sayHello();

【讨论】:

    【解决方案2】:

    您可以使用原型继承。

    var Foo = function(){
        this.hello = function(){
            console.log('hello');
        }
    }
    
    var Bar = function(){
        Foo.call(this);
    }
    
    Bar.prototype = Object.create(Bar);
    
    new Bar().hello();
    

    【讨论】:

      【解决方案3】:

      Javascript 语言中没有任何东西直接支持这一点,但可以模拟。

      var Foo = {
          hello: function() {
              console.log("hello");
          }
      };
      
      function Bar() {}
      
      for (var key in Foo) {
          Bar.prototype[key] = Foo[key];
      }
      
      new Bar().hello(); // prints hello
      

      这会遍历Foo 中的所有内容并将其添加到Bar

      【讨论】:

      • 谢谢,但我需要一些与 ES6 类一起使用的东西
      • @user3309314 是 Foo 一个类还是像我这样的普通对象?
      • 它是一个类而不是一个普通的对象
      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2012-04-04
      相关资源
      最近更新 更多