浏览 3433 次
锁定老帖子 主题:对prototype使用上的几点不理解
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-06-27
这是我的autocreateselect.js var AutoCreateSelect = Class.create(); AutoCreateSelect.prototype = { initialize : function(url,src,des){ this._url = url; this._src = $(src); this._des = $(des); this._src.onchange = function(){this._createAjax()}.bind(this); //this._src_onchange = this._createAjax().bind(this);这样写报尚未实现的错误 }, _createAjax : function(){ if(this._src.options[this._src.selectedIndex].text!='请选择'){ new Ajax.Request(this._url,{ parameters:{id:this._src.value}, onComplete:(function(res){this._createSelect(res.responseText)}).bind(this) }); } }, _createSelect :function(resultText){ try{ var jObject = resultText.evalJSON(true); this._des.options.length = 0; this._des.disabled = false; jObject.option.each(function(i){this._des.add(new Option(i.text,i.value))}.bind(this)).bind(this); }catch(e){ alert(e.message); //这里有两个问题 //e本身应该有个e.name但我死活打不出来,我用for(in)都能打出来就是alert(e.name)不行 //第2个就是jObject.option.each这句老是抛属性不存在的异常,我怎么改都抛,但却能执行,看了源码但这没看懂 } } }弄了天都不行,希望大家指点指点,谢谢 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-06-27
//this._src_onchange = this._createAjax().bind(this);这样写报尚未实现的错误
应该为 this._src_onchange = this._createAjax.bind(this); 下面的那个错误最好把完整页面,js,还有你那个返回的json打包一起上传,别人才能知道问题所在。 还有不要到处bind(this),看的人头晕。明显你没有明白什么javascript中函数为一等公民的作用。 |
|
返回顶楼 | |
发表时间:2007-06-27
谢谢楼上,真不好意思我没看到过关于js函数的一等公民的问题,希望能给点资料给个介绍的连接也行,
each的那句话绑定那么多this是因为我在eahc的function的this不是我这个类的,您有更好的方法,能给写个例子吗, 这是我的工程他是eclipse下的,就是实现一个级联菜单的功能 |
|
返回顶楼 | |
发表时间:2007-06-28
|
|
返回顶楼 | |
发表时间:2007-07-09
我把我提的问题都改正了,一下是改正后的代码
var AutoCreateSelect = Class.create(); AutoCreateSelect.prototype = { initialize : function(url,src,des){ this._url = url; this._src = $(src); this._des = $(des); this._src.onchange = this._createAjax.bind(this); }, _createAjax : function(){ if(this._src.options[this._src.selectedIndex].text!='请选择'){ new Ajax.Request(this._url,{ parameters:{id:this._src.value}, onComplete:this._createSelect.bind(this) }); } }, _createSelect :function(transport){ try{ var jObject = transport.responseText.evalJSON(true); this._des.options.length = 0; this._des.disabled = false; jObject.option.each(function(i){this._des.add(new Option(i.text,i.value))}.bind(this)); }catch(e){ alert('后台数据格式错误');} } } |
|
返回顶楼 | |