javascript问题中的var keywork [英] var keywork in javascript problem

查看:49
本文介绍了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 use var 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


这篇关于javascript问题中的var keywork的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆