这是一个有趣的问题,为了完全理解它,最好查看 JavaScript表达式。基本上,JavaScript中的赋值看起来像
variable_name = expression
当你创建那个
变量
时,
表达式
会被评估。
//so this
number = 3 * 5
//is the same as
number = 15
函数
可以通过表达式、字面量(如字符串
或整数
)或变量名进行调用。
// '|' means 'or'
function(expression | literal | variable)
如果你将一个表达式传递给一个函数
function(expression)
,那么这个
expression
会首先被计算,然后再传递到函数中。
// so
function(3*5)
//is the same as
function(15)
同样的规则也适用于函数调用。如果一个函数在另一个函数内被调用,它会首先被执行,并且其结果将成为外部函数的参数。
让我们看一个例子
function increment(number){
return number + 1
}
n = 1
document.write(increment(n))
首先,使用参数increment(n)
和n = 1
调用document.write
increment(n) = increment(1) = 2
document.write(increment(n))
document.write(2)
希望这有所帮助!
编辑:
回到您的例子。
function multiNum(x,y){
return x*y
}
var num = multiNum(3,4) // num = 12
//so
document.write(num)
//is the same as
document.write(12)
a(b(c(42, c('x'))))
这样的代码。这更难以阅读(哪个右括号对应哪个左括号?),将其分解成具有良好命名的变量会使维护变得更加容易。 - blex