Javascript"这个"变量混乱

Ass*_*sky 0 javascript jquery function object

我目前正在阅读"Javascript:The Good Parts"这本书并正在使用函数.我制作了一个测试脚本来测试一些属性,我对结果感到有些困惑.这是代码:

<h3>Object</h3>
        <div style="padding-left: 10px;">
            <script type="text/javascript">
                function outterF()
                {
                    document.writeln("outterF.this = " + this + "<br>");

                    function innerF() 
                    {
                        document.writeln("innerF.this = " + this + "<br>");
                        return this;
                    };

                    var inner = innerF();
                    return this;
                }

                document.writeln("<b>From Inside:</b><br>");
                var outF = outterF();
                var inF = outF.inner;

                document.writeln("<br>");
                document.writeln("<b>From Outside:</b><br>");
                document.writeln("outterF.this = " + outF + "<br>");
                document.writeln("innerF.this = " + inF + "<br>");
            </script>
        </div>
Run Code Online (Sandbox Code Playgroud)

结果是:

Object
From Inside:
outterF.this = [object Window]
innerF.this = [object Window]

From Outside:
outterF.this = [object Window]
innerF.this = undefined
Run Code Online (Sandbox Code Playgroud)

请注意,outF.inner返回"undefined",是某种语言错误吗?
显然,outF.inner指向与我的对象无关的Window对象,但它不应该至少指向一个Function对象吗?

谢谢 -
阿萨夫

Ivo*_*zel 7

this就是确定价值的方式.

// function case
foo(); // this inside foo will refer to the global object

// method case
test.foo(); // this inside foo will refer to test

// constructor case
new foo(); // this inside foo will refer to the newly created object
Run Code Online (Sandbox Code Playgroud)

因此,除非你处理方法或构造函数this是相当无用的.