下面的代码运行正常:

代码1:

  1. <html>  
  2. <head>  
  3.    <title>this指obj</title>  
  4. </head>  
  5. <body>  
  6. <script>  
  7. <!--  
  8.  
  9.     function Utility(){   
  10.         this.decode = function(str){ return unescape(str); };   
  11.         this.getCookie = function(key){ // ... 省略提取cookie字符串的代码  
  12.             var value = "i%27m%20a%20cookie";         
  13.             return this.decode(value);   
  14.         }   
  15.     };   
  16.    alert((new Utility).getCookie()); 
  17. -->  
  18. </script>  
  19. </body>  
  20. </html>  

上面的代码修改如下,出错,不能运行:

  1. <html>  
  2. <head>  
  3.    <title>this指obj</title>  
  4. </head>  
  5. <body>  
  6. <script>  
  7. <!--   
  8.     function Utility(){   
  9.         this.decode = function(str){ return unescape(str); };   
  10.         this.getCookie = function(key){ // ... 省略提取cookie字符串的代码  
  11.             var value = "i%27m%20a%20cookie";         
  12.             return decode(value);   
  13.         }   
  14.     };   
  15.    alert((new Utility).getCookie()); 
  16. -->  
  17. </script>  
  18. </body>  
  19. </html>  

 报错信息:

decode is not defined

return decode(value);

代码1可以修改成如下的形式:

  1. <html>   
  2. <head>   
  3.    <title>this指obj</title>   
  4. </head>   
  5. <body>   
  6. <script>   
  7. <!--   
  8.   
  9.     var Utility = {    
  10.         decode:function(str){ return unescape(str); },    
  11.         getCookie:function(key){ // ... 省略提取cookie字符串的代码   
  12.             var value = "i%27m%20a%20cookie";          
  13.             return Utility.decode(value);    
  14.         }    
  15.     };  
  16.    alert(Utility.getCookie());  
  17. -->   
  18. </script>   
  19. </body>   
  20. </html>  

 

在使用Extjs4的过程中遇到了同样的问题,一时蒙了,代码如下:

lesson02_06.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5. <title>Ext.create</title> 
  6. <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/> 
  7. <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script> 
  8. <script type="text/javascript" src="indexDemo6.js"></script> 
  9. </head> 
  10. <body> 
  11. </body> 
  12. </html> 

indexDemo6.js

  1. (function(){ 
  2.     Ext.onReady(function(){ 
  3.     Ext.define("myWin",{ 
  4.       extend:'Ext.window.Window'
  5.           width:400, 
  6.           height:300, 
  7.           title:'uspcat'
  8.           newtitle:'new uspcat'
  9.           mySetTitle:function(){ 
  10.             this.title = this.newtitle; 
  11.           }, 
  12.       initComponent: function(){ 
  13.             mySetTitle(); 
  14.         this.callParent(arguments); 
  15.       } 
  16.     }); 
  17.         Ext.create('myWin',{ 
  18.       title:'my win' 
  19.     }).show(); 
  20.     }); 
  21. })(); 
 

运行结果如下:

mySetTitle is not defined

mySetTitle();

indexDemo6.js修改如下,mySetTitle();修改成:this.mySetTitle();

  1. (function(){ 
  2.     Ext.onReady(function(){ 
  3.     Ext.define("myWin",{ 
  4.       extend:'Ext.window.Window'
  5.           width:400, 
  6.           height:300, 
  7.           title:'uspcat'
  8.           newtitle:'new uspcat'
  9.           mySetTitle:function(){ 
  10.             this.title = this.newtitle; 
  11.           }, 
  12.       initComponent: function(){ 
  13.             this.mySetTitle(); 
  14.         this.callParent(arguments); 
  15.       } 
  16.     }); 
  17.         Ext.create('myWin',{ 
  18.       title:'my win' 
  19.     }).show(); 
  20.     }); 
  21. })(); 

-------------------------------------------------------------------------------

  1. <html>  
  2. <head>  
  3.    <title>this指obj</title>  
  4. </head>  
  5. <body>  
  6. <script>  
  7. <!--  
  8.     function Utility(){   
  9.         this.decode = function(str){ return unescape(str); };   
  10.         this.getCookie = function(key){ // ... 省略提取cookie字符串的代码  
  11.             var value = "i%27m%20a%20cookie";         
  12.             return this.decode(value);   
  13.         }   
  14.     };   
  15.    function showUserIdentity(){   
  16.         // 保存getCookie函数到一个局部变量,因为下面会经常用到  
  17.         var getCookie = Utility.getCookie; //不能成功赋值,因为Utility是一个方法 
  18.      alert(getCookie);  
  19.     } 
  20.   showUserIdentity(); 
  21. -->  
  22. </script>  
  23. </body>  
  24. </html>  

运行结果:

undefined 

111