Javascript 赋值语法
我在上遇到了以下代码Javascript 赋值语法,javascript,syntax,Javascript,Syntax,我在上遇到了以下代码 我不熟悉这种语法。它到底实现了什么?此赋值后points变量将包含哪些内容?points变量是一个空数组。以下结果是一个,允许显示多个声明,并用逗号分隔 var points = [], // Gives us a power of 2 based on our width power = Math.pow(2, Math.ceil(Math.log(width) / (Math.log(2)))), seed = seed || { s:
我不熟悉这种语法。它到底实现了什么?此赋值后points变量将包含哪些内容?points变量是一个空数组。以下结果是一个,允许显示多个声明,并用逗号分隔
var points = [],
// Gives us a power of 2 based on our width
power = Math.pow(2, Math.ceil(Math.log(width) / (Math.log(2)))),
seed = seed || {
s: height / 2 + (Math.random() * displace * 2) - displace,
e: height / 2 + (Math.random() * displace * 2) - displace
};
它被视为使用单个变量语句,选择使用哪种形式是一种风格偏好。(我选择后者,jslint建议选择前者。)
需要注意的一件有趣的事情是var seed=seed | | |…
,其中seed
已经是一个参数。这是因为var
不像在C语言中那样“定义”变量,而是声明应用了范围广泛的注释。因此,整个范围内只有一个seed
变量,而var
'再次使用它没有什么区别-它过去是,将来也永远是一个局部变量
请参阅,以了解
seed | | |..
的一般用法。假设您指的是seed=seed | |{
object literal}
,目的是在调用函数时没有该参数(或该参数未定义,或任何其他错误值)
如果存在seed,它将具有传入的值,但如果它未定义(或null、0、false等),则将为其提供一个对象。您是说
seed=seed ||{
我猜这不是整个函数,并且在某个点上向数组中添加了一些东西?你到底不熟悉什么?你是如何尝试研究你不熟悉的东西的?我想他是在尝试研究他不熟悉的东西。嗯,根据规范,从技术上讲,这是一个声明。它只是pr有点不自然differently@FelixKling谢谢你一直提醒我。
var points = [],
// Gives us a power of 2 based on our width
power = Math.pow(2, Math.ceil(Math.log(width) / (Math.log(2)))),
seed = seed || {
s: height / 2 + (Math.random() * displace * 2) - displace,
e: height / 2 + (Math.random() * displace * 2) - displace
};
var points = [];
var power = Math.pow(2, Math.ceil(Math.log(width) / (Math.log(2))));
var seed = seed || {
s: height / 2 + (Math.random() * displace * 2) - displace,
e: height / 2 + (Math.random() * displace * 2) - displace
};