te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Why are arguments in JavaScript not preceded by the var keyword? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why are arguments in JavaScript not preceded by the var keyword? - Stack Overflow

programmeradmin3浏览0评论

This may be a silly question, but why are function arguments in JavaScript not preceded by the var keyword?

Why:

function fooAnything(anything) {
  return 'foo' + anyThing;
}

And not:

function fooAnything(var anything) {
  return 'foo' + anyThing;
}

I have a feeling the answer is because that's what the Spec says but still...

This may be a silly question, but why are function arguments in JavaScript not preceded by the var keyword?

Why:

function fooAnything(anything) {
  return 'foo' + anyThing;
}

And not:

function fooAnything(var anything) {
  return 'foo' + anyThing;
}

I have a feeling the answer is because that's what the Spec says but still...

Share Improve this question asked Nov 29, 2011 at 12:11 helpermethodhelpermethod 62.2k71 gold badges197 silver badges280 bronze badges 4
  • Why do you want the keyword there? var declares variables, parameters are (a bit) different. There is no point of the keyword there, it adds no value. – user1046334 Commented Nov 29, 2011 at 12:14
  • isn't that a reason good enough? what additional information will var give you? besides, you save 4 chars everytime you declare a function var which you can put on making the variable name longer for clarity :). – Asken Commented Nov 29, 2011 at 12:14
  • 1 Because ECMA-262 says formal parameters, like declared variables, are made named properties of the local variable object before any code is executed. So a formal parameter is more or less declared by default. – RobG Commented Nov 29, 2011 at 12:24
  • 2 It's not "because that's what the Spec says" - there is a reason why the spec says that: because it makes sense to do it that way. Requiring "var" would be redundant, and making it optional would be confusing. (JavaScript does have some other features that in my opinion are pretty arbitrary and fall into the "because that's what the spec says" category.) – nnnnnn Commented Nov 29, 2011 at 12:37
Add a ment  | 

4 Answers 4

Reset to default 9

Most dynamicaly-typed programming languages don't have explicit vars in the argument list. The purpose of the var keyword is to differentiate between "I am setting an existing variable" and "I am creating a new variable" as in

var x = 17; //new variable
x = 18; //old variable

(Only few languages, like Python, go away with the var pletely but there are some issues with, for example, closures and accidental typos so var is still widespread)

But in an argument list you cannot assign to existing variables so the previous ambiguity does not need to be resolved. Therefore, a var declaration in the arguments list would be nothing more then redundant boilerplate. (and redundant boilerplate is bad - see COBOL in exibit A)


You are probably getting your idea from C or Java but in their case type declarations double up as variable declarations and the cruft in the argument lists is for the types and not for the declarations.

We can even get away with a "typeless, varless" argument list in some typed languages too. In Haskell, for example, types can be inferred so you don't need to write them down on the argument list. Because of this the argument list consists of just the variable names and as in Javascript's case we don't need to put the extraneous "let" (Haskell's equivalent to "var") in there:

f a b =  --arguments are still variables,
         -- but we don't need "let" or type definitions
   let n = a + b in  --extra variables still need to be declared with "let"
   n + 17

It would be a redundant use of the var keyword. Items that appear in the parentheses that follow a function name declaration are explicitly parameters for the function.

The var keyword declares the scope of the variable. A function argument also introduces the scope for that argument. Hence there's no need for it, since it serves the same function.

I think the question es up because we're used to seeing function bla (int i) in many languages. The same, syntactically, as the int i; somewhere in the function body to declare a variable. The two ints are however not doing the same; the first defines the type, the second defines type and the scope. If you don't have to declare the type, the scope-declaration still needs to happen (which is why we have var in the second case) but there is no information needed in front of arguments.

发布评论

评论列表(0)

  1. 暂无评论