Skip to content

Commit

Permalink
feat(jQuery): add Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 30, 2015
1 parent d36019c commit b3b727e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
36 changes: 35 additions & 1 deletion ja/jQuery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,38 @@ jQueryでは`$.fn`を拡張する事で、`$()`の返り値であるjQueryオブ
<script src="greenify.js"></script>
```

## どういう仕組み?
## どういう仕組み?

## 実装してみよう

`calculator`という拡張可能な計算機をjQuery Pluginと同じ方法で作ってみたいと思います。

`calculator` は以下のような形となります。

[import, calculator.js](../../src/jQuery/calculator.js)

`$.fn`と同様に`prototype`へのaliasを貼っているだけのただのコンストラクタ関数です。

```
calculator.fn = calculator.prototype;
```

`calculator(初期値)`と書けるようにしているため、少し特殊なコンストラクタとなっていますが、この拡張の仕組みとは関係ないのでとりあえず置いておきましょう。

[calculator.js](#calculator.js)には何も実装が入ってないので、プラグインで四則演算の実装を追加してみます。

[import, calculator-plugin.js](../../src/jQuery/calculator-plugin.js)

[calculator-plugin.js](#calculator-plugin.js)では、`calculator.fn.add`というように`add`というメソッドを追加しています。

また、モジュールで依存関係を示していますがやっていることはjQueryと同じで、[calculator.js](#calculator.js)を読み込んでから[calculator-plugin.js](#calculator-plugin.js)を読み込んでいるだけですね。

```html
<script src="jquery.js"></script>
<script src="greenify.js"></script>
```

これを使うと`calculator#add`といったメソッドが利用できるようになるので、以下のように書くことが出来ます。

[import, calculator-example.js](../../src/jQuery/calculator-example.js)

7 changes: 7 additions & 0 deletions src/jQuery/Calculator-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
function Calculator(value) {
this.value = value;
}
// alias
Calculator.fn = Calculator.prototype;
export default Calculator;
7 changes: 7 additions & 0 deletions src/jQuery/calculator-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
import assert from "assert";
import calculator from "./calculator";
import "./calculator-plugin"; // Extend

var resultValue = calculator(0).add(10).multi(10).value;
assert.equal(resultValue, 10 * 10);
18 changes: 18 additions & 0 deletions src/jQuery/calculator-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
import calculator from "./calculator";
calculator.fn.add = function (x) {
this.value += x;
return this;
};
calculator.fn.sub = function (x) {
this.value -= x;
return this;
};
calculator.fn.multi = function (x) {
this.value *= x;
return this;
};
calculator.fn.div = function (x) {
this.value /= x;
return this;
};
10 changes: 10 additions & 0 deletions src/jQuery/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
function calculator(value) {
if (!(this instanceof calculator)) {
return new calculator(value);
}
this.value = value;
}
// alias
calculator.fn = calculator.prototype;
export default calculator;
27 changes: 27 additions & 0 deletions test/jQuery/calculator-plugin-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// LICENSE : MIT
"use strict";
import assert from "power-assert";
import calculator from "../../src/jQuery/calculator";
import "../../src/jQuery/calculator-plugin";
describe("calculator-plugin", function () {
describe("#add", function () {
it("should add value", function () {
assert.equal(calculator(0).add(10).value, 10);
});
});
describe("#sub", function () {
it("should subtraction value", function () {
assert.equal(calculator(0).sub(10).value, -10);
});
});
describe("#multi", function () {
it("should multiply value", function () {
assert.equal(calculator(0).add(10).multi(10).value, 10 * 10);
});
});
describe("#div", function () {
it("should subtraction value", function () {
assert.equal(calculator(0).add(10).div(2).value, 10 / 2);
});
});
});

0 comments on commit b3b727e

Please sign in to comment.