// 警告 Object.setPrototypeOf()可能会严重影响代码性能。 // Mozilla文档说得很清楚:“在所有浏览器和JavaScript引擎中,修改继承关系的影响都是微妙且深远的。这种影响并不仅是执行Object.setPrototypeOf()语句那么简单,而是会涉及所有访问了那些修改过[[Prototype]]的对象的代码。” // 为避免使用Object.setPrototypeOf()可能造成的性能下降,可以通过Object.create()来创建一个新对象,同时为其指定原型 let biped = { numLegs: 2 } let person = Object.create(biped) person.name = 'Matt' console.log(person.name) // Matt console.log(person.numLegs) // 2 console.log(person.getPrototypeOf(person) === biped) // true // ECMAScript 5通过增加Object.create()方法将原型式继承的概念规范化了。这个方法接收两个参数:作为新对象原型的对象,以及给新对象定义额外属性的对象(第二个可选)。在只有一个参数时,Object.create()与这里的object()方法效果相同 // 原型式继承非常适合不需要单独创建构造函数,但仍然需要在对象间共享信息的场合。但要记住,属性中包含的引用值始终会在相关对象间共享,跟使用原型模式是一样的。
inspect
1 2 3 4 5
// util.inspect.custom support for node 6+ /* istanbul ignore else */ if (util.inspect.custom) { this[util.inspect.custom] = this.inspect; }
1 2 3 4 5 6 7 8 9 10 11 12
// 看下node源码 v16.3.0 lib/internal/util/inspect.js /** * Echos the value of any input. Tries to print the value out * in the best way possible given the different types. * * @param {any} valueThevaluetoprintout. * @param {Object} optsOptionaloptionsobjectthatalterstheoutput. */ /* Legacy: value, showHidden, depth, colors */ function inspect(value, opts) { ... } inspect.custom = customInspectSymbol;
nodemon
基本介绍:nodemon用来监视node.js应用程序中的任何更改并自动重启服务
https://github.com/remy/nodemon nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
开发阶段,依赖于nodemon监测代码变动,自动重启node.js应用; 生产环境,通过pm2,cluster模式,按cpu核数启动对应进程数(The cluster mode allows networked Node.js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications.) The cluster module allows easy creation of child processes that all share server ports.
// koa lib/application.js use 的实现 /** * Use the given middleware `fn`. * * Old-style middleware will be converted. * * @param {Function} fn * @return {Application} self * @api public */
use(fn) { if(typeoffn !== 'function') thrownewTypeError('middleware must be a function!'); if (isGeneratorFunction(fn)) { deprecate('Supportfor generators will be removed in v3. ' + 'See the documentation for examples of how to convert old middleware ' + 'https://github.com/koajs/koa/blob/master/docs/migration.md'); fn = convert(fn); } debug('use %s', fn._name || fn.name || '-'); this.middleware.push(fn); returnthis; }
If you want to see a benchmark comparison with the most commonly used routers, see [here](https://github.com/delvedor/router-benchmark).<br> Do you need a real-world example that uses this router? Check out [Fastify](https://github.com/fastify/fastify) or [Restify](https://github.com/restify/node-restify).