javascript问题中的var keywork
问题描述:
每个伙伴
我是Java脚本中的新手,并且想知道javascript中的var关键字用于什么用途?
用于定义变量或范围概念吗?请举个例子.
Thanks
hi every buddy
im new in java script and a want to know that var keyword in javascript use for what?
is using for define variable or scope concept?please give me an example .
thanks
答
它定义了一个变量,该变量有效地将其范围定义为当前块.
如果使用它,它将始终使用给定名称创建一个新变量,如果不使用它,则它将备份当前作用域,直到找到已经具有该名称的变量并使用它为止.或创建一个新变量(如果找不到).
我建议您尽可能使用var
,因为如果您不打算在两个不同的函数中使用相同名称的全局变量,那么它将防止以后出现错误!
It defines a variable, which effectively defines it''s scope as the current block.
If you use it, then it always creates a new variable with the given name, if you don''t, then it looks back up the current scope(s) until it finds a variable with that name already and uses that instead. Or creates a new variable if it doesn''t find one.
I would recommend that you usevar
whenever you can, as it prevents mistakes later if you didn''t mean to use a global variable of the same name in two different functions!
这些是一些链接,您可以检查一下.
benalman.com
最佳实践
developer.mozilla.org/zh-CN /docs/JavaScript
www.tizag.com
these are some links, you can check it.
benalman.com
bestpractices
developer.mozilla.org/en-US/docs/JavaScript
www.tizag.com