npm link
如果开发一个模块test,在example里引用这个模块,每次test模块的变动都需要反映到example里;可以使用npm link命令;
把test链接到全局模式下:
cd ~/memo/test //进入test模块下
npm link //’/Users/username/memo/test’ -> ‘/usr/local/lib/node_modules/test’
把test引用到example项目中:
cd ~/memo/example
npm link test //把全局模式的模块链接的本地;
npm link test 命令会去/usr/local/lib/node_modules下查找test模块,
找到以后把’/usr/loc...
对象
使用{}和defineProperty默认属性是不一样的;
var obj={a:1}
Object.getOwnPropertyDescriptor(obj,'a')
//{value: 1, writable: true, enumerable: true, configurable: true}
Object.defineProperty(obj,'b',{value:2})
Object.getOwnPropertyDescriptor(obj,'b')
//{value: 2, writable: false, enumerable: false, configurable: false}
obj.c=123
Object.getOwnPropertyDescrip...
模块化
CommonJS
核心思想是通过 require 方法来同步加载依赖的其他模块,通过 module.exports 导 出需 要暴露的接口。
CommonJS 的优点在于:
• 代码可复用于 Node.js 环境下井运行,例如做同构应用:
• 通过Npm发布的很多第三方模块都采用了 CommonJS规范。
CommonJS 的缺点在于,这样的代码无法直接运行在浏览器环境下,必须通过工具转换 成标准的 ES5。
CommonJS 还可以细分为 CommonJSl 和 CommonJS2,区别在于 CommonJSl 只能通过 exports.XX = XX 的方式导出,
而 CommonJS2 在 CommonJSl 的基础上加入了 module.exports = XX 的导...
共计 109 篇文章,22 页。