これは、オブジェクトを作成するのには問題ありません。もしあなたがmongoのようなDBを使い始めるなら、あなたはmongooseでオブジェクトを作るほうが良いかもしれませんが、それは個人的な好みです。あなたの例のためとして -
1)輸出者
module.exports = Person;
2)輸入者、別のファイルから
const Person = require('../path/to/Person');
3))(非常に重要なコンストラクタを呼び出すためにnew
キーワードで人を作成します。
const mitch = new Person('Mitch');
javascript's prototype
にお読みください。すべてのオブジェクトにはObject.prototype
への参照があります。そして、あなたがオブジェクトを作成し、参照がHere's an example from MDN
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
console.log('Is rect an instance of Rectangle?',
rect instanceof Rectangle); // true
console.log('Is rect an instance of Shape?',
rect instanceof Shape); // true
rect.move(1, 1); // Outputs, 'Shape moved.'
Object.create(obj)
しようとmodelarに渡されているように、新しいオブジェクトのプロトタイプを割り当てることObject.create(obj)
でオブジェクトを作成することができ、それがモデルを定義するクラスを使用しています。 http://modelar.hyurl.com。 Personを定義する方法は古いスタイルです。クラスを試してみてください。 –