理解javascript函数和var

时间:2013-09-13 23:17:06

标签: javascript

我刚刚通过课堂笔记,并没有真正理解

var x = 1;
function func1() 
{
    x+= 10; 
}
func2 = function( x ) 
{ 
    x += 5;
}

行func2 = function(x)是什么意思? x会是15吗?

1 个答案:

答案 0 :(得分:0)

当您将作为基元的参数传递给函数时,它们将按值传递。但是如果传入一个作为对象的参数,它将通过引用传递。

function myfunction(x)
{
    // x is equal to 4
    x = 5;
   // x is now equal to 5
}

var x = 4;
alert(x); // x is equal to 4

myfunction(x); 
alert(x); // x is still equal to 4

function myobject()
{
    this.value = 5;
}

var o = new myobject();
alert(o.value); // o.value = 5

function objectchanger(fnc)
{
    fnc.value = 6;
}

objectchanger(o);
alert(o.value); // o.value is now equal to 6