Javascript简写 - 数组

时间:2016-10-09 11:12:50

标签: javascript

请告诉我,这里发生了什么?

Array(+(r=readline)())

' +'的含义是什么?在这个声明中?

由于

1 个答案:

答案 0 :(得分:0)

它使用readline()函数的结果创建一个新数组,并将r设置为对同一函数的引用

var readline = function() {
    return "3";
}

var arr = Array(+(r=readline)());

console.log(arr); // an array with 3 undefined

将其分解一点

var readline = function() {
    return "3";
}

var r;

var x = (r = readline);// returns "readline" as assigning always returns assigned value
var y = +x(); // returns whatever "readline" returns, and coerces into a number (3)
    
var arr = Array(y); // creates a number, "y" is now the number 3

console.log(arr); // an array with 3 undefined