Node.js (main.js):
var my = require('./count.js');
var foo = new my.foo(10, 2);
foo.plus().say();
foo.times().say();
exports js (count.js):
var foo = function(m, n) {
this.m = m;
this.n = n;
this.answer = 0;
this.method = "";
};
foo.prototype.plus = function() {
this.answer = this.m + this.n;
this.method = "+";
return this;
};
foo.prototype.times = function() {
this.answer = this.m * this.n;
this.method = "*";
return this;
};
foo.prototype.say = function() {
console.log("%d %s %d = %d", this.m, this.method, this.n, this.answer);
return this;
};
exports.foo = foo;
範例結果:說明:
在Node.js中採用流暢介面寫法,並使用exports建立一個外部js,在主程式中require後使用。
沒有留言:
張貼留言