zoukankan      html  css  js  c++  java
  • javascript之函数作用域

    function中的魔鬼this,我们来说说。

      function demo(){

        conlose.log(this);

      }

    当我们调用demo()的时候,这里面的this指的是window这个对象。[如果是严格模式下this是undefined]

    有些时候对象a有某个方法[比如是say],而对象b没有这个方法,但是我就想借用一下[不要在b上添加这个方法]

      var a = {

        name:'a',

        say:function(){

          conlose.log(this.name);

        }

      };

      var b = {

        name:'a'

      }

      我们可以这样使用 a.say.bind(b)();或a.say.call(b);或a.say.apply(b);

      这里的this就是作用域,bing只是改变作用域,参数传递跟原方法一样。call和apply调用方法时,第一个参数$this是作用域this,

      call传参数是 $this,a1,a2,a3....

      apply传参数是 $this,[a1,a2,a3....]

      

      

  • 相关阅读:
    C#深入浅出 修饰符(二)
    HDU 5785 Interesting
    HDU 5783 Divide the Sequence
    HDU 5781 ATM Mechine
    UVA 714 Copying Books
    uva 1471 Defense Lines
    UVA 11134 Fabled Rooks
    UVA 11572 Unique Snowflakes
    UVA 11093 Just Finish it up
    UVA 10954 Add All
  • 原文地址:https://www.cnblogs.com/ehuanrum/p/6626516.html
Copyright © 2011-2022 走看看