<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Function Context Example</title>
    <script>
      var o1 = {handle:'o1'};
      var o2 = {handle:'o2'};
      var o3 = {handle:'o3'};
      window.handle = 'window';      function whoAmI() {
        return this.handle;
      }      o1.identifyMe = whoAmI;      alert(whoAmI());
      alert(o1.identifyMe());
      alert(whoAmI.call(o2));
      alert(whoAmI.apply(o3));    </script>
  </head>  <body>
  </body></html>上面是一个页面源码,请帮忙解释一下

解决方案 »

  1.   

    window.handle = 'window';//定义window对象的handle属性,值为window字符串o1.identifyMe = whoAmI;//定义o1对象的identifyMe属性,值为whoAmI函数
      

  2.   

    "handle"和"identifyMe"都是自定义吗?
    function whoAmI() { 
      return this.handle; 

    这个里面的this只待什么,帮忙好好说说用法,谢谢!
      

  3.   

    考察你对于this和上下文(context)的理解。alert(whoAmI());// 默认的上下文就是window, 返回undefined
    alert(o1.identifyMe()); // 返回o1, 因为此时的上下文o1,this指o1对象
    alert(whoAmI.call(o2)); // 返回o2,考察利用call函数临时创建上下文
    alert(whoAmI.apply(o3));// 返回o3,考察利用apply函数临时创建上下文
      

  4.   

    这段代码就是考你this关键字的指向在JS中this可以理解为当前运行的对象
    alert(whoAmI());//this指向window
    alert(o1.identifyMe()); // this指向o1
    alert(whoAmI.call(o2)); // this指向o2
    alert(whoAmI.apply(o3));// this指向o3