English 中文(简体)
[关闭]
原标题:Traits in javascript [closed]
最佳回答
function Trait (methods) {
  this.traits = [methods];
};

Trait.prototype = {
    constructor: Trait

  , uses: function (trait) {
      this.traits = this.traits.concat (trait.traits);
      return this;
    }

  , useBy: function (obj) {
      for (var i = 0; i < this.traits.length; ++i) {
        var methods = this.traits [i];
        for (var prop in methods) {
          if (methods.hasOwnProperty (prop)) {
            obj [prop] = obj [prop] || methods [prop];
          }
        }
      }
    }
};

Trait.unimplemented = function (obj, traitName) {
  if (obj === undefined || traitName === undefined) {
    throw new Error ("Unimplemented trait property.");
  }
  throw new Error (traitName + " is not implemented for " + obj);
};

示例:

var TEq = new Trait ({
    equalTo: function (x) {
      Trait.unimplemented (this, "equalTo");
    }

  , notEqualTo: function (x) {
      return !this.equalTo (x);
    }
});

var TOrd = new Trait ({
    lessThan: function (x) {
      Trait.unimplemented (this, "lessThan");
    }

  , greaterThan: function (x) {
      return !this.lessThanOrEqualTo (x);
    }

  , lessThanOrEqualTo: function (x) {
      return this.lessThan (x) || this.equalTo (x);
    }

  , greaterThanOrEqualTo: function (x) {
      return !this.lessThan (x);
    }
}).uses (TEq);


function Rational (numerator, denominator) {
  if (denominator < 0) {
    numerator *= -1;
    denominator *= -1;
  }
  this.numerator = numerator;
  this.denominator = denominator;
}

Rational.prototype = {
    constructor: Rational

  , equalTo: function (q) {
      return this.numerator * q.numerator === this.denominator * q.denominator;
    }

  , lessThan: function (q) {
      return this.numerator * q.denominator < q.numerator * this.denominator;
    }
};

TOrd.useBy (Rational.prototype);

var x = new Rational (1, 5);
var y = new Rational (1, 2);

[x.notEqualTo (y), x.lessThan (y)]; // [true, true]
问题回答

各种办法不同,同时也制作现成的图书馆。

混合是各阶级等级之间最古老的代码再利用形式,需要按线性顺序组成,因为混合的概念不包括/承认解决冲突的功能。

轨迹是精细的代码再利用单位,在班级一级工作;但是它们比较灵活,因为Traits必须提供合成操作器,供混合、排除或别名的方法使用。

我建议阅读两篇论文,

  1. A fresh look at JavaScript Mixins by Angus Croll from May 2011
  2. The many talents of JavaScript for generalizing Role Oriented Programming approaches like Traits and Mixins from April 2014.

纯粹功能和以授权为基础的混合机械学与下面的2个实例一样简单明了.。

var Enumerable_first = function () {
  this.first = function () {
    return this[0];
  };
};
var list = ["foo", "bar", "baz"];

console.log("(typeof list.first)", (typeof list.first)); // "undefined"

Enumerable_first.call(list); // explicit delegation

console.log("list.first()", list.first()); // "foo"

...第一个例子在“参与”一级采取行动,第二个例子涵盖“类”一级......。

var Enumerable_first_last = function () {
  this.first = function () {
    return this[0];
  };
  this.last = function () {
    return this[this.length - 1];
  };
};
console.log("(typeof list.first)", (typeof list.first));  // "function"   // as expected
console.log("(typeof list.last)", (typeof list.last));    // "undefined"  // of course

Enumerable_first_last.call(Array.prototype);  // applying behavior to [Array.prototype]

console.log("list.last()", list.last());      // "baz"  // due to delegation automatism

如果需要建立和(或)制作现成图书馆,就应更仔细地研究

  1. traits.js
  2. CocktailJS

长长

附录一附录一的附录一

另见:

附录一附录一的附录一I

由于我有时显然对这件事情不屑一顾,我想加上一些最后的想法.。

在没有太多胶水代码(如上所述)的情况下,图书馆的不可知性方法只能用于极精细的可折合成的行为再利用单位。 因此,只要不超过1或2个容易解决的冲突,以Angus Crolls Flight Mixins 为基础的模式就是可遵循的途径。

如果涉及到真正的特性,则必须有一个抽象的层次。 这个层次( 例如, 提供某种像 DSL 那样的合成糖) 需要隐藏复杂性, 比如, 特性的合成特征或冲突解决的特性在时间上( 当特性的行为被应用到对象/类型时) 。

到目前为止,索尔有3个例子,从我的角度看,确切地说明了业务方案所要求的.。

我怎样才能在印记里写出特质呢?

org/traitsjs' rel=“nofollow”>trait.js 图书馆。它们也有关于一般模式及其具体执行的相当好的文章。我最近在我的项目中建了起来,它很有魅力。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签