定义控件如下,有两种不同写法,其一:
this.dwbmCombobox = new Ext。
this.Grid = new Ext.Grid.gridpanel({
    tbar: [{
        handler: onSave
    }]
})
onSave: function(){
    var dw = this.dwbmCombobox.getValue();
}
form = function(){
items: [this.dwbmCombobox,
this.Grid]
};
var eForm = new form()
其二:
this.dwbmCombobox = new Ext。
this.Grid = new Ext.Grid.gridpanel({
    tbar: [{
        handler: onSave
    }]
})
function onSave(){
    var dw = this.dwbmCombobox.getValue();
}
this.form = new Ext.Panel({
items:[]
});
请问为什么第一种写法save方法可以访问到dwbmCombobox,而第二种方法中就访问不到?第二种方法下save方法的this为Grid,第一种方法的this就不是Grid了吗?求教高手

解决方案 »

  1.   

    this指代当前对象,不管你那种写法,只看调用你的save方法的是哪个对象。
      

  2.   

    this.dwbmCombobox = new Ext。
    this.Grid = new Ext.Grid.gridpanel({
      tbar: [{
      handler: onSave.call(this)
      }]
    })
    function onSave(){
      var dw = this.dwbmCombobox.getValue();
    }
    this.form = new Ext.Panel({
    items:[]
    });
    第二种 改成红色的代码 应该就行了~·
      

  3.   

    this.Grid = new Ext.Grid.gridpanel({
      tbar: [{
      handler: function(){onSave.call(this)}
      }]
    })
      

  4.   

    楼主的第一种方法的代码写的有点问题 不知是单单贴出了一段呢还是...
    this.dwbmCombobox = new Ext。
    this.Grid = new Ext.Grid.gridpanel({
      tbar: [{
      handler: onSave
      }]
    })
    onSave: function(){
      var dw = this.dwbmCombobox.getValue();
    }
    form = function(){
    items: [this.dwbmCombobox,
    this.Grid]
    };
    var eForm = new form()
    请注意红色部分 这个是json对象的写法 和你的上下文明显不一致请求楼主将代码贴全 因为不贴全this作用域我们无法正确判断 代码长也没关系 先贴出来
      

  5.   

    好像是不太一样……,方法一源码
    createForm: function() {
    this.sbbmText = new Ext.form.TextField({
                fieldLabel: '设备编码',
                name: 'sbbm',
                readOnly: false,
                selectOnFocus: true,
                allowBlank: true,
                anchor: '90%'
            });
    this.firstGrid = new Ext.grid.GridPanel({
                id: "lgrid1",
                ddGroup: 'secondGridDDGroup',
                store: this.firstGridStore,
                columns: sbcols,
                width: 880,
                height: 180,
                loadMask: (
          { msg: '正在加载数据……' }
          ),
                tbar: [
             {
                 id: 'addButton',
                 text: '添加到清单',
                 iconCls: 'add',
                 tooltip: '添加到清单',
                 handler: this.onSave,
                 scope: this
             }
            ],
                //分页
                bbar: new Ext.PagingToolbar({
                    //pageSize: 1000,
                    pageSize: 25,
                    store: this.firstGridStore,
                    displayInfo: true,
                    emptyMsg: "当前沒有数据!    "
                })
            });
    },onSave: function() {}
      

  6.   

    var me = this;//将this提出成变量 否则由于作用域的问题会改变其指代对象
    me.dwbmCombobox = new Ext。me.Grid = new Ext.Grid.gridpanel({
    tbar : [{
    handler : onSave
    }]
    })
    onSave = function()
    {
    //注意 原来楼主如果是this的话 在tbar中其指代的是tbar里的那个对象 而不是最外边的this 所以楼主拿不到dwbmComboBox
    //改为了me的话 me永远指向最外边的this 就可以拿到了
    var dw = me.dwbmCombobox.getValue(); 
    }
    form = function()
    {
    items : [me.dwbmCombobox, me.Grid]
    };
    var eForm = new form()// 其二:
    var me = this;
    me.dwbmCombobox = new Ext。me.Grid = new Ext.Grid.gridpanel({
    tbar : [{
    handler : onSave
    }]
    })
    function onSave()
    {
    var dw = me.dwbmCombobox.getValue();
    }
    me.form = new Ext.Panel({
    items : []
    });
    我将楼主的方法1和2都改了下 
    所以我们在做Ext的项目时 这种this指代是相当多的 一定要注意其作用域 
      

  7.   

    晕~~楼主早发代码啊 害得我自己瞎估计 呵呵 
    看了楼主的源码针对这个说下 你就明白了 
    为方便说明 外边我加了a对象 在你的Ext中应该就是一个namespace了的一个对象
    var a = {
    createForm : function()
    {
    var me = this;//将this指代提出me变量 这样me永远指向a对象
    this.sbbmText = new Ext.form.TextField({
    fieldLabel : '设备编码',
    name : 'sbbm',
    readOnly : false,
    selectOnFocus : true,
    allowBlank : true,
    anchor : '90%'
    });
    this.firstGrid = new Ext.grid.GridPanel({
    id : "lgrid1",
    ddGroup : 'secondGridDDGroup',
    store : this.firstGridStore,//这里的this不是a
    columns : sbcols,
    width : 880,
    height : 180,
    loadMask : ({
    msg : '正在加载数据……'
    }),
    tbar : [{
    id : 'addButton',
    text : '添加到清单',
    iconCls : 'add',
    tooltip : '添加到清单',
    handler : this.onSave,//因为有了scope参数 所以其指代是a对象 我们通常不要这样 直接用me代替
    scope : this
    }],
    // 分页
    bbar : new Ext.PagingToolbar({
    // pageSize: 1000,
    pageSize : 25,
    store : this.firstGridStore,//这里this也不是a
    displayInfo : true,
    emptyMsg : "当前沒有数据!    "
    })
    });
    },
       //这个onSave明显是a对象的方法 
    onSave : function()
    {
    }
    }
      

  8.   

    ext学起来容易否?需要什么基础?求指点