Node js:best to define entity class

Source: Internet
Author: User

?

If you start-to-use a-DB like MONGO, you might is better off creating objects with mongoose and that ' s personal preference as well. As for your example-

1) Export person

Module.exports = person;

2) Import person from another file

Const person = require ('.. /path/to/person ');

3) Create person with the? New ? keyword to call the constructor (very important)

cons T Mitch = newperson ( ' Mitch '

You Should read up on? javascript ' s prototype object.prototype object.create (obj) ?to Create objects and assign the new object ' s prototype as the reference being passed in to? object.create (obj)

Here's an example from MDN

Shape-superclass

functionShape() {

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

functionRectangle() {

Shape. Call (this); //Call Super constructor.

}

?

Subclass extends Superclass

Rectangle. prototype = Object. Create (Shape. prototype);

Rectangle. prototype. Constructor = Rectangle;

?

var rect = newRectangle();

?

Console.log (' is rect of instance of Rectangle? ' ,

Rect instanceofRectangle); //True

Console.log (' is rect of instance of Shape? ' ,

Rect instanceofShape); //True

Rect.move (1, 1); //Outputs, ' Shape moved. '

?

From:https://stackoverflow.com/questions/47006288/node-js-best-way-to-define-entity-class

Node js:best to define entity class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.