<tfoot id='jzZzW'></tfoot>
  • <legend id='jzZzW'><style id='jzZzW'><dir id='jzZzW'><q id='jzZzW'></q></dir></style></legend>

    <small id='jzZzW'></small><noframes id='jzZzW'>

      1. <i id='jzZzW'><tr id='jzZzW'><dt id='jzZzW'><q id='jzZzW'><span id='jzZzW'><b id='jzZzW'><form id='jzZzW'><ins id='jzZzW'></ins><ul id='jzZzW'></ul><sub id='jzZzW'></sub></form><legend id='jzZzW'></legend><bdo id='jzZzW'><pre id='jzZzW'><center id='jzZzW'></center></pre></bdo></b><th id='jzZzW'></th></span></q></dt></tr></i><div id='jzZzW'><tfoot id='jzZzW'></tfoot><dl id='jzZzW'><fieldset id='jzZzW'></fieldset></dl></div>

          <bdo id='jzZzW'></bdo><ul id='jzZzW'></ul>
      2. 为什么这个代码指向窗口对象?

        why the code this point to window object?(为什么这个代码指向窗口对象?)

      3. <legend id='3hg4J'><style id='3hg4J'><dir id='3hg4J'><q id='3hg4J'></q></dir></style></legend>
          <i id='3hg4J'><tr id='3hg4J'><dt id='3hg4J'><q id='3hg4J'><span id='3hg4J'><b id='3hg4J'><form id='3hg4J'><ins id='3hg4J'></ins><ul id='3hg4J'></ul><sub id='3hg4J'></sub></form><legend id='3hg4J'></legend><bdo id='3hg4J'><pre id='3hg4J'><center id='3hg4J'></center></pre></bdo></b><th id='3hg4J'></th></span></q></dt></tr></i><div id='3hg4J'><tfoot id='3hg4J'></tfoot><dl id='3hg4J'><fieldset id='3hg4J'></fieldset></dl></div>
            <tbody id='3hg4J'></tbody>

          <small id='3hg4J'></small><noframes id='3hg4J'>

              <tfoot id='3hg4J'></tfoot>
                <bdo id='3hg4J'></bdo><ul id='3hg4J'></ul>
                1. 本文介绍了为什么这个代码指向窗口对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的代码是:

                  var length = 20;
                  function fn(){
                      console.log(this.length);
                  }
                  
                  var o = {
                      length:10,
                      e:function (fn){
                         fn();
                         arguments[0]();
                      }
                  }
                  
                  o.e(fn);
                  

                  输出是20,1,谁能告诉我为什么?

                  the output is 20,1,who can tell me why?

                  推荐答案

                  this 关键字出现在函数内部时,其值取决于函数的调用方式.

                  When the this keyword occurs inside a function, its value depends on how the function is called.

                  在您的情况下,调用 fn() 时未提供 this 值,因此默认值为 window.使用 arguments[0](),上下文是 arguments 对象,其长度为 1.

                  In your case, fn() is called without providing the a this value, so the default value is window. With arguments[0](), the context is the arguments object, whose length is 1.

                  关键是函数在哪里被调用并不重要,重要的是函数如何被调用.

                  The point is it does not matter where the function is called, but it matters how the function is called.

                  var length = 20;
                  function fn(){
                      console.log(this.length);
                  }
                  
                  var o = {
                      length:10,
                      e:function (fn){
                         fn(); // this will be the window.
                         arguments[0](); // this will be arguments object.
                      }
                  }
                  
                  o.e(fn);
                  

                  此外,如果您希望 this 成为对象 o,您可以使用 callapply, 或者先绑定一个对象.

                  Further more, if you want this to be the object o, you could use call or apply, or bind an object first.

                  var length = 20;
                  function fn(){
                      console.log(this.length);
                  }
                  
                  var o = {
                      length:10,
                      e:function (fn){
                         var fn2 = fn.bind(this);
                         fn.call(this); // this in fn will be the object o.
                         fn.apply(this); // this in fn will be the object o.
                         fn2(); // this also will be the object o.
                      }
                  }
                  
                  o.e(fn);
                  

                  这篇关于为什么这个代码指向窗口对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                    <bdo id='faexj'></bdo><ul id='faexj'></ul>

                    <i id='faexj'><tr id='faexj'><dt id='faexj'><q id='faexj'><span id='faexj'><b id='faexj'><form id='faexj'><ins id='faexj'></ins><ul id='faexj'></ul><sub id='faexj'></sub></form><legend id='faexj'></legend><bdo id='faexj'><pre id='faexj'><center id='faexj'></center></pre></bdo></b><th id='faexj'></th></span></q></dt></tr></i><div id='faexj'><tfoot id='faexj'></tfoot><dl id='faexj'><fieldset id='faexj'></fieldset></dl></div>
                    <legend id='faexj'><style id='faexj'><dir id='faexj'><q id='faexj'></q></dir></style></legend>
                  • <small id='faexj'></small><noframes id='faexj'>

                      <tbody id='faexj'></tbody>
                      <tfoot id='faexj'></tfoot>