Javascript函数参数参数或全局变量
如何知道我在函数中使用的Javascript函数参数参数或全局变量,javascript,variables,arguments,Javascript,Variables,Arguments,如何知道我在函数中使用的x是参数还是全局变量 如果我提供了这个 var x=10; function foo(){ x=120; alert(arguments[0]); //outputs 10 } foo(x); 这两个代码之间的区别是什么,幕后发生了什么,数组参数是如何变化的 function foo(x){ x=120; console.log(arguments[0]);//logs 120 } foo(x); 现在经过修改: var x=10; /
x
是参数还是全局变量
如果我提供了这个
var x=10;
function foo(){
x=120;
alert(arguments[0]); //outputs 10
}
foo(x);
这两个代码之间的区别是什么,幕后发生了什么,数组参数是如何变化的
function foo(x){
x=120;
console.log(arguments[0]);//logs 120
}
foo(x);
现在经过修改:
var x=10; // this is variable of window namespace
function foo(x){
// It will make variable of this functions. Every function make its own namespace.
// So now you have x in local namespace, so you can't touch x from global
// namespace, becouse you gave them same names. With for example function foo(a),
// global x would be still accessible.
x=120;
// Here, you change value of x. Program will ask the deepest namespace if there
// is any x variable. If not, it will continue upper and upper. Right now,
// we have local x variable, so it will change value of local variable, which
// will extinct at the end of our function. The most upper namespace is global
// namespace. One more deeper is window namespace.
alert(arguments[0]);
// arguments is Array, which every function has as its property. Via arguments,
// you can touch every argument, that is passed to function. Later, I will
// show you an example
}
foo(12);
// finaly here, you call function foo and you set local x = 12. But then,
// you rewrite local x, so it is 120. After all, you alert first argument,
// which is local x, which is 120.
有争论的不同故事
var x=10;
function foo(a){ // now we have different name for local variable
x=120; // so our x is x from window namespace
alert(arguments[0]); // now output is a, which will be 12
alert(x); // and x is 120
}
foo(12);
alert(x); // x will be still 120...
基本上,参数函数属性允许我们执行黑魔法。它使代码不易理解,但它非常强大。很少情况下,使用它们是个好主意。明智地使用它们
参数和局部变量指向内存中的同一位置。所以改变一个会立刻改变另一个
我如何知道我在函数中使用的x是否是一个参数
还是全局变量
通过检查函数的参数列表。如果它提到一个参数x
,如下所示:
var x=10;
function foo(){ // we didnt declare any local variable
x=120; // so our x is x from window namespace again
alert(arguments[0]);
// output is 12, becouse we can touch arguments via
// arguments array, even if we didnt declare any local variables
}
foo(12);
然后您就知道x
是一个参数。它是参数[0]
的别名(另一个名称),因为它是第一个参数。参数
对象不是实际数组。它具有名称为数字的属性,并且具有长度属性,因此它看起来类似于数组,但缺少数组的其他特性
您可以将参数
视为函数调用中传递的值的最终存储。参数名是获取存储在参数中的值的方便方法
如果您的功能开始:
function foo(x) {
那么它就没有参数了。函数中引用的任何内容都必须是以另一种方式定义的值。它可以是函数内部声明的局部变量:
function foo() {
function foo() {
var x = 10;
或者它可以引用在函数外部声明的名称:
function foo() {
function foo() {
var x = 10;
在没有“严格模式”的JavaScript中,可以说:
var x;
function foo() {
x = 10;
从未在任何地方声明过var x。结果是x将被创建为一个全局变量(或全局对象的属性,例如,在浏览器中,它将创建窗口.x
),这是一个很大的错误源,因为您可能拼错了变量名,而没有注意到您正在为一个不存在的变量赋值。在我的ubunut 12-chromium中,它提醒120:)您使用什么浏览器?。我还通过控制台在Chrome 28中获得了120
。我还通过控制台和在IE9、Firefox 22和Chrome 28中看到了120
。我编辑了我的问题我发布了答案,但在此期间,您更改了您的问题。我希望,这将帮助你理解无论如何。。。