下面的代码运行正常:
代码1:
- <html>
- <head>
- <title>this指obj</title>
- </head>
- <body>
- <script>
- <!--
- function Utility(){
- this.decode = function(str){ return unescape(str); };
- this.getCookie = function(key){ // ... 省略提取cookie字符串的代码
- var value = "i%27m%20a%20cookie";
- return this.decode(value);
- }
- };
- alert((new Utility).getCookie());
- -->
- </script>
- </body>
- </html>
上面的代码修改如下,出错,不能运行:
- <html>
- <head>
- <title>this指obj</title>
- </head>
- <body>
- <script>
- <!--
- function Utility(){
- this.decode = function(str){ return unescape(str); };
- this.getCookie = function(key){ // ... 省略提取cookie字符串的代码
- var value = "i%27m%20a%20cookie";
- return decode(value);
- }
- };
- alert((new Utility).getCookie());
- -->
- </script>
- </body>
- </html>
报错信息:
decode is not defined return decode(value); |
代码1可以修改成如下的形式:
- <html>
- <head>
- <title>this指obj</title>
- </head>
- <body>
- <script>
- <!--
- var Utility = {
- decode:function(str){ return unescape(str); },
- getCookie:function(key){ // ... 省略提取cookie字符串的代码
- var value = "i%27m%20a%20cookie";
- return Utility.decode(value);
- }
- };
- alert(Utility.getCookie());
- -->
- </script>
- </body>
- </html>
在使用Extjs4的过程中遇到了同样的问题,一时蒙了,代码如下:
lesson02_06.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Ext.create</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="indexDemo6.js"></script>
- </head>
- <body>
- </body>
- </html>
indexDemo6.js
- (function(){
- Ext.onReady(function(){
- Ext.define("myWin",{
- extend:'Ext.window.Window',
- width:400,
- height:300,
- title:'uspcat',
- newtitle:'new uspcat',
- mySetTitle:function(){
- this.title = this.newtitle;
- },
- initComponent: function(){
- mySetTitle();
- this.callParent(arguments);
- }
- });
- Ext.create('myWin',{
- title:'my win'
- }).show();
- });
- })();
运行结果如下:
mySetTitle is not defined mySetTitle(); |
indexDemo6.js修改如下,mySetTitle();修改成:this.mySetTitle();
- (function(){
- Ext.onReady(function(){
- Ext.define("myWin",{
- extend:'Ext.window.Window',
- width:400,
- height:300,
- title:'uspcat',
- newtitle:'new uspcat',
- mySetTitle:function(){
- this.title = this.newtitle;
- },
- initComponent: function(){
- this.mySetTitle();
- this.callParent(arguments);
- }
- });
- Ext.create('myWin',{
- title:'my win'
- }).show();
- });
- })();
-------------------------------------------------------------------------------
- <html>
- <head>
- <title>this指obj</title>
- </head>
- <body>
- <script>
- <!--
- function Utility(){
- this.decode = function(str){ return unescape(str); };
- this.getCookie = function(key){ // ... 省略提取cookie字符串的代码
- var value = "i%27m%20a%20cookie";
- return this.decode(value);
- }
- };
- function showUserIdentity(){
- // 保存getCookie函数到一个局部变量,因为下面会经常用到
- var getCookie = Utility.getCookie; //不能成功赋值,因为Utility是一个方法
- alert(getCookie);
- }
- showUserIdentity();
- -->
- </script>
- </body>
- </html>
运行结果:
undefined
111