简体   繁体   English

这在Javascript“ typeof(arguments [0])”中是什么意思

[英]what this means in Javascript “typeof(arguments[0])”

I have found this in my code, probably someone has done prior to me. 我已经在代码中找到了这个,可能有人在我之前做过。 I am unable to get what exactly this line of code does. 我无法获得此代码行的确切功能。 what will arguments[0] do here. arguments [0]在这里会做什么。

        typeof(arguments[0])

Entire code is this: 整个代码是这样的:

 var recommendedHeight = (typeof(arguments[0]) === "number") ? arguments[0] : null;

The problem is I always get recommendedHeight as null . 问题是我总是将recommendedHeightnull any idea when this returns any other value ? 知道什么时候返回任何其他值?

Every function in JavaScript automatically receives two additional parameters: this and arguments . JavaScript中的每个函数都会自动接收两个附加参数: thisarguments The value of this depends on the invocation pattern, could be the global browser context (eg, window object), function itself, or a user supplied value if you use .apply() . this取决于调用模式,可能是全球浏览器上下文(例如,窗口对象),函数本身,或用户提供的价值,如果你使用.apply() The arguments parameter is an array-like object of all parameters passed into the function. arguments参数是传递给函数的所有参数的类似数组的对象。 For example if we defined the following function.. 例如,如果我们定义以下函数。

function add(numOne, numTwo) {
  console.log(arguments);
  return numOne + numTwo;
} 

And used it like so.. 并像这样使用它。

add(1, 4);

This would return 5 of course, and also show the arguments array in the console [1, 4] . 当然,这将返回5,并在控制台[1, 4]显示arguments数组。 What this allows you to do is pass and access more parameters than those defined by your function, powerful stuff. 这允许您执行的操作是传递和访问比功能强大的功能定义的参数更多的参数。 For instance.. 例如..

add(1, 4, "extra parameter 1", "extra parameter 2", "extra parameter n");

We would see in the console [1, 4, "extra parameter 1", "extra parameter 2", "extra parameter n"] . 我们将在控制台中看到[1, 4, "extra parameter 1", "extra parameter 2", "extra parameter n"] Now in our function we could access "extra parameter 1" via arguments[2] . 现在在函数中,我们可以通过arguments[2]访问"extra parameter 1"

Your code checks the type of the first item in the arguments array (eg, number, string, etc) and it does so using a ternary operator. 您的代码检查参数数组中第一项的类型(例如,数字,字符串等),并使用三元运算符进行检查。

Expanding your code may make it more clear: 扩展您的代码可以使其更加清晰:

var recommendedHeight;

//if the first argument is a number
if ( typeof(arguments[0]) === "number" ) {
  //set the recommendedHeight to the first argument passed into the function
  recomendedHeight = arguments[0];
} else {
  //set the recommended height to null
  recomendedHeight = null;
}

Hope that helps! 希望有帮助!

That means: 这意味着:

If the variable type of arguments[0] is a number, then recommendedHeight gets the value of arguments[0], otherwise set it to null. 如果arguments [0]的变量类型是数字,则RecommendationHeight获取arguments [0]的值,否则将其设置为null。

Probably arguments is an array containing some attributes, and its first record should contain the recommended height. 可能的参数是一个包含一些属性的数组,并且其第一条记录应包含建议的高度。 This is why it should be a number. 这就是为什么它应该是一个数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM