4

调用我的函数后,我很惊讶我的函数没有抛出错误,因为有一个变量并且使用了一个未在函数范围内定义的变量。

我的问题:为什么我的函数不抛出未定义、错误等?它不应该因为“长度”不在我的函数参数中而引发错误吗?我知道,如果我将“长度”切换为“高度”,它将起作用。

如果有人可以逐步解释javascript如何解释这个函数,这将对我有所帮助。这是我的代码:

function areaRectangle(width, height) {
   aRectangle = width * length;
   console.log("The area of the rectangle is: " + aRectangle);
}
areaRectangle(10, 7); # The area of the rectangle is: 0
4

7 回答 7

6

因为它不是使用参数或变量,而是使用恰好为 0 的window.length属性。

于 2014-12-29T04:07:04.190 回答
5

这取决于您在哪个环境中执行此功能。

如果您在浏览器中执行此操作,似乎全局上下文中有一个长度属性,其计算结果为数值。当您在控制台中执行函数并尝试访问长度时,由于未在函数中定义长度,JavaScript 引擎将在全局上下文中查找长度变量(因为闭包)。

如果您在 Node 中执行此操作,您会注意到抛出错误,因为 Node 中的全局上下文没有长度属性。

于 2014-12-29T04:09:42.030 回答
1

length derives from window.length and that is a property that defines how many frames are within a given window context. 0 = no frames, 1 = 1 frame, N = N frames.

https://developer.mozilla.org/en-US/docs/Web/API/Window.length

This is not to be confused with the given property of Object.length, since window inherits from Object (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Properties).

于 2014-12-29T04:15:01.343 回答
1

当您没有在当前代码块中声明它时,您访问最接近的相关父范围变量,因此当您引用时length,如果不存在已声明的变量,则在大多数执行环境中它被解析为全局范围。在上下文所在的浏览器中window,您实际上是在引用window.length.

意识到这个答案有点令人费解,下面是一个如何解析本地或父范围变量的示例。

如果你这样做了,

<script>
    var length = "potato";
    function areaRectangle(width, height) {
        aRectangle = width * length;
        console.log("The area of the rectangle is: " + aRectangle);
    }
    areaRectangle(10, 7); // My number results in 0
</script>

你会在控制台中得到这个;

矩形的面积为:NaN

所以如果你这样做了

<script>
    var length = "potato";
    function areaRectangle(width, height) {
		var length = height;
        aRectangle = width * length ;
        console.log("The area of the rectangle is: " + aRectangle);
    }
    areaRectangle(10, 7); // results is 70
</script>

你会在控制台中得到这个:

矩形的面积是:70

这是因为变量length是在本地或另一个更近的可见范围内声明的。

在您的代码中,变量被绑定到全局对象(window在浏览器中),导致window.length.

于 2014-12-29T04:11:55.803 回答
1

您尚未在函数内部本地定义长度变量。因此,当您调用此函数时,它会查找其父范围以查找长度变量的值。

假设这个函数是在全局范围内定义的,那么它将查找窗口对象以找到长度的值。在这种情况下,这将是 window.length,它指的是当前窗口中的 iframe 数量。您可以在此处阅读有关 window.length的更多信息:

话虽如此,我们可以通过修改您的原始功能来解决此问题。由于您的函数试图计算矩形的面积,我们需要修改您的原始函数,以便它使用您的两个参数计算距离。我建议执行以下操作:

function areaRectangle(width, height) {
  aRectangle = width * height; // notice we changed the second variable to height
  console.log("The area of the rectangle is: " + aRectangle);
}
areaRectangle(10, 7); // This will result in 70.
于 2014-12-29T04:09:21.790 回答
0

length is 0 at this time. It also happens to exist in global scope (window). I think you may have confused your variable names.

You are allowed to use the variable named length as an argument, but you don't do that here, so it uses the one in the global scope.

This is what you want:

function areaRectangle(width, height) 
{
    aRectangle = width * height;

    console.log("The area of the rectangle is: " + aRectangle);
}

areaRectangle(10, 7);
于 2014-12-29T04:16:14.733 回答
0

You try using use strict:

function areaRectangle(width, height) {
  'use strict';
  aRectangle = width * length;
  console.log("The area of the rectangle is: " + aRectangle);
}
areaRectangle(10, 7);
Open console...

于 2014-12-29T05:06:35.570 回答