express 4 这句源码怎么解释?

2014-06-17 23:03:12 +08:00
 shin
// set .cache unless explicitly provided
opts.cache = null == opts.cache
? this.enabled('view cache')
: opts.cache;


express 4.x application.js app.render()方法中的设置。
3453 次点击
所在节点    Node.js
8 条回复
ijse
2014-06-17 23:14:18 +08:00
除非明确指定是否启用cache, 默认启用cache
shin
2014-06-17 23:24:52 +08:00
@ijse

可否这样理解?
opts.cache = (null == opts.cache) ? this.enabled('view cache') : opts.cache;

如果上文 opts.cache 为 undefined,则opts.cache = this.enabled('view cache');

然后this.enabled('view cache')返回this。

则opts.cache = this; 通过上下文可知this为application对象。
emric
2014-06-18 03:03:39 +08:00
kfll
2014-06-18 10:56:41 +08:00
enabled 返回的是布尔值
ijse
2014-06-18 12:32:23 +08:00
@shin 就是除非明确指定禁用cache,否则都将启用cache
hegfirose
2014-06-18 13:26:02 +08:00
var cacheOption = (null == opts.cache) ? this.enabled('view cache') : opts.cache;

opts.cache = cacheOption;

如果没有设置 opts.cache (opts.cache 为 undefined, undefined == null),则获取 this.enabled('view cache') 的值,否则直接使用 opts.cache 设置的值

赋值的时候先执行 = 右边的运算
cyr1l
2014-06-20 00:50:42 +08:00
opts.cache = null == opts.cache? this.enabled('view cache'): opts.cache;

opts.cache = (null == opts.cache? this.enabled('view cache'): opts.cache;)

opts.cache = ((null == opts.cache)? this.enabled('view cache'): opts.cache;)

coffee :
opts.cache = if opts.cache then this.enabled('view cache') else opts.cache

JavaScript:
opts.cache = if (null == opts.cache){
return this.enabled('view cache');
}else{
return opts.cache
}
ChiChou
2014-06-30 13:49:38 +08:00
为何不写成

opts.cache = opts.cache || this.enabled('view cache');

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/118065

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX