权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>Considering best practices, can we use double unary operator ! in JavaScript? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Considering best practices, can we use double unary operator ! in JavaScript? - Stack Overflow

programmeradmin10浏览0评论

For my understanding the unary ! operator performs implicit type conversions, and are sometimes used for type conversion.

So basically the ! operator converts its operand to a boolean and negates it.

Now:

!!x // Same as Boolean(x)

In fact:

!!'true' === Boolean('true') // true

So I am assuming both !!x and Boolean(x) perform the same action.

I would like to know:

  • Do you know any caveats making my assumptions wrong?
  • Which way should be preferred in term of good practice?
  • Do you know any differences to be aware among different ECMAScript versions or Browser vendors?

For my understanding the unary ! operator performs implicit type conversions, and are sometimes used for type conversion.

So basically the ! operator converts its operand to a boolean and negates it.

Now:

!!x // Same as Boolean(x)

In fact:

!!'true' === Boolean('true') // true

So I am assuming both !!x and Boolean(x) perform the same action.

I would like to know:

  • Do you know any caveats making my assumptions wrong?
  • Which way should be preferred in term of good practice?
  • Do you know any differences to be aware among different ECMAScript versions or Browser vendors?
Share Improve this question edited Jul 1, 2015 at 8:21 godfrzero 2,2701 gold badge26 silver badges32 bronze badges asked Jul 1, 2015 at 7:45 GibboKGibboK 74k147 gold badges451 silver badges674 bronze badges 6
  • 1 Why do you keep referring to it as unary? Is there a binary ! operator? – BoltClock Commented Jul 1, 2015 at 7:53
  • 1 @BoltClock thanks for your ment, for my understanding ! is a unary operator. I found this reference in JavaScript: The Definitive Guide, 6th Edition. – GibboK Commented Jul 1, 2015 at 7:58
  • 5 IMO it doesn't matter. They are the same in this case. I would prefer double-negate operator !! because it is less typing. Just bang bang and you are done. But some would say that Boolean() casting function is more human readable. – Blago Eres Commented Jul 1, 2015 at 8:15
  • 1 the same thing is true for !!'false' === true because as far as I know strings with a value other than '' are always true – nils Commented Jul 1, 2015 at 8:40
  • 2 @Anand it's not "the same as not using it". var k = 'hey you'; console.log(k === !!k); will print "false". This is all about type, the point is to get a boolean value based on the content of the operand, which might be anything from a number to an arbitrary object. – ttzn Commented Jul 1, 2015 at 9:28
 |  Show 1 more ment

2 Answers 2

Reset to default 7
  • Your assumptions are correct. That is exactly how it works and I'm not aware of any special care to be taken when using this.

  • Speaking of good practice, you'll probably have as many people supporting the quick 'n "dirty" !! way as there are who would advocate using the Boolean function ; however, from my experience, it appears that !! is way more mon in library code (look at the jQuery source, you have a lot of stuff like return !!locked;). IMHO, it is sufficiently recognizable to be used without degrading code readability.

  • This has all been standard for a very long time in ECMAScript ; I can't speak for old versions of Internet Explorer (before IE 8), but you can pretty much trust all modern browsers to behave the same in this case.

Useful references in the standard :

  • http://www.ecma-international/ecma-262/5.1/#sec-11.4.9
  • http://www.ecma-international/ecma-262/5.1/#sec-9.2

Do you know any caveats making my assumptions wrong?

Your assumption is correct. Both Boolean function and !! has the same functionality.

Which way should be preferred in term of good practice?

In terms of performance, Boolean is now globally faster than unary operators

JS fiddle link: https://jsfiddle/vj593auw/1/

Do you know any differences to be aware among different ECMAScript versions or Browser vendors?

Boolean and unary operators are implemented from JavaScript 1.0, so they should be available in all browsers that supports JavaScript.

发布评论

评论列表(0)

  1. 暂无评论