1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 5 <title>改变作用域</title> 6 <script type="text/javascript"> 7 var o1={ handle:'xxxx'}; 8 function whoAmI(){ 9 return this.handle; 10 } 11 o1.identifyMe=whoAmI; 12 console.log(o1.identifyMe()); 13 console.log(whoAmI.call(o1)); 14 console.log(whoAmI.apply(o1)); 15 </script> 16 </head> 17 <body> 18 </body> 19 </html>
通过apply或者call改变作用域,三者的结果是一样的,都会输出xxxx