Node js : Best way to define entity class
If you start to use a DB like mongo, you might be better off creating objects with mongoose but 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)
const mitch = new
Person('Mitch');
You should read up on javascript's prototype. Every object has a reference to Object.prototype. Then you can create objects with Object.create(obj) to create objects and assign the new object's prototype as the reference being passed in to Object.create(obj)
// 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.'
From: https://stackoverflow.com/questions/47006288/node-js-best-way-to-define-entity-class
Node js : Best way to define entity class的更多相关文章
- Node.js学习笔记——Node.js开发Web后台服务
一.简介 Node.js 是一个基于Google Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效.Node.j ...
- 在node.js中,使用基于ORM架构的Sequelize,操作mysql数据库之增删改查
Sequelize是一个基于promise的关系型数据库ORM框架,这个库完全采用JavaScript开发并且能够用在Node.JS环境中,易于使用,支持多SQL方言(dialect),.它当前支持M ...
- (翻译)《Hands-on Node.js》—— Why?
事出有因 为何选择event loop? Event Loop是一种推进无阻塞I/O(网络.文件或跨进程通讯)的软件模式.传统的阻塞编程也是用一样的方式,通过function来调用I/O.但进程会在该 ...
- ECMAScript Web APIs node.js
https://hacks.mozilla.org/2015/04/es6-in-depth-an-introduction/ What falls under the scope of ECMASc ...
- Node.js与Sails~项目结构与Mvc实现
回到目录 Sails是一个Node.js的中间件架构,帮助我们很方便的构建WEB应用程序,网址:http://www.sailsjs.org/,它主要是在Express框架的基础上发展起来的,扩展了新 ...
- Node.js入门:前后端模块的异同
通常有一些模块可以同时适用于前后端,但是在浏览器端通过script标签的载入JavaScript文件的方式与Node.js不同.Node.js在载入到最终的执行中,进行了包装,使得每个文件中的 ...
- node.js使用汇总贴
金天:学习一个新东西,就要持有拥抱的心态,如果固守在自己先前的概念体系,就会有举步维艰的感觉..NET程序员初用node.js最需要适应的就是异步开发,以及弱类型语言难以避免的拼写错误与弱小的语法提示 ...
- 了不起的Node.js读书笔记
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 第二章 Js概览 基于GoogleV8引擎 Object.keys(o) 数组方法:遍历forEach.过滤filter ...
- [译]Node.js : Building RESTful APIs using Loopback and MySQL
国庆后可能就要使用StrongLoop那套东西来做项目了 原文:http://www.javabeat.net/loopback-mysql/ Loopback是什么? Loopback是一个开源的N ...
随机推荐
- sqlserver2014内存数据库特性介绍
sql server 2014提供了众多激动人心的新功能,但其中我想最让人期待的特性之一就要算内存数据库了,下面就简单介绍一下sql server 2014的内存数据库的一些特性 相信大家对内存数 ...
- GetBuiltProjectOutputRecursive error running Xamarin Forms iOS on Visual Studio
Seems like I get this weird problem while running Xamarin.iOS on Visual studio. This happened after ...
- python脚本从excel表到处数据,生成指定格式的文件
#coding:gbk #导入处理excel的模块 import xlrd #定义哪些字段须要推断,仅仅支持时间字段 toSureColArray = ['CREATE_TIME','MODIFY_T ...
- Redis源代码分析(三十三)--- redis-cli.cclient命令行接口的实现(2)
今天学习完了命令行client的兴许内容,总体感觉就是环绕着2个东西转,config和mode.为什么我会这么说呢,请继续往下看,client中的配置结构体和之前我们所学习的配置结构体,不是指的同一个 ...
- 窗体的Alpha通道透明色支持
参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...
- C#实现缩放字体
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- spring4 quartz2 集群动态任务
实现定时任务的执行,而且要求定时周期是不固定的.测试地址:http://sms.reyo.cn 生产环境:nginx+tomcat+quartz2.2.1+spring4.2.1 集群. 实现功能:可 ...
- 详细解读Android中的搜索框(四)—— Searchable配置文件
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android=" ...
- JAVA中String.format的用法 转16进制,还可以补0
1.对整数进行格式化:%[index$][标识][最小宽度]转换方式 我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...
- 深入JVM系列(二)之GC机制、收集器与GC调优
一.回想JVM内存分配 须要了解很多其它内存模式与内存分配的,请看 深入JVM系列(一)之内存模型与内存分配 1.1.内存分配: 1.对象优先在EDEN分配 2.大对象直接进入老年代 3.长期存活的 ...