js对象内部如何调用该对象在prototype中定义的方法????

kofyf 2008-08-18 10:24:02
想实现如下功能:
<script>
function b(){
this.b1();
}
b.prototype.b1 = function(){
alert(1111);
};

</script>

<a href="#" onclick="b();">test</a><br>
高人请指点一下。主要是为了重载b1()方法.
这样做的原因是因为b()是定义在外部js中。在页面调用b() 默认情况下不用处理b1(),但是特定情况下需要在b1()中做操作。
当然可以这样处理:
<script>
function b(){
try{
b1();
}catch(exception){}
}
function b1(){
}
</script>
<a href="#" onclick="b();">test</a><br>
但是感觉不是很优雅。
...全文
811 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chen32384778 2008-08-20
  • 打赏
  • 举报
回复
很简单吧 之前定义一个外层的函数
var afterClosed = function(){};
function _closeDiv(id){
_showDiv(id, false);
_showDiv(id+"_close", false);
fixSelectBug(true);
afterClosed();
}

var afterClosed = function(){
alert('sadsa');
};
kofyf 2008-08-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chen32384778 的回复:]
直接调用b()而不是new对象的时候里面的this就是指的window对象了 就可以省略 即
[/Quote]
解释很清晰哈。发个实际的代码。某些方法没有。主要是给个实际运用中的例子


/////////////////////////////部门员工树///////////
function createDiv( obj, id ){
var closeDiv = document.createElement("div");
window.document.body.appendChild(closeDiv);
closeDiv.id = id + "_close";
closeDiv.style.position = "absolute";
closeDiv.style.textAlign = 'right';
closeDiv.style.zIndex = 100;
closeDiv.style.visibility = 'hidden';
closeDiv.style.borderColor = '#FFFFFF';
closeDiv.style.border = '0px';
closeDiv.style.borderColor = '#000000';
closeDiv.style.borderStyle = 'solid';
closeDiv.innerHTML = "<a href=javascript:_closeDiv('"+id+"'); class='btnCls2' title='关闭' ></a>";

var topDiv = document.createElement("div");
window.document.body.appendChild(topDiv);
topDiv.id = id;
topDiv.style.zIndex = 99;
topDiv.style.display = 'block';
topDiv.style.position = "absolute";
topDiv.style.backgroundColor = '#FFFFFF';
topDiv.style.border = '1px';
topDiv.style.borderColor = '#000000';
topDiv.style.borderStyle = 'solid';
topDiv.style.fontSize = '12px';
topDiv.style.padding = '0px';
topDiv.style.visibility = 'hidden';
topDiv.style.overFlow = 'true';

_moveDiv(obj, id);
}

function _moveDiv(obj, id){

$(id).style.width= getClientWidth(obj) + "px";
$(id).style.height= getClientHeight(obj) + "px";
$(id+"_close").style.width= 21 + "px";
$(id+"_close").style.height= 18 + "px";
var x = 0;
var y = 0;
if(is_ie){
x= document.body.scrollLeft + obj.getBoundingClientRect().left;
y=document.body.scrollTop + obj.getBoundingClientRect().top + obj.clientHeight + 3;
$(id).style.filter = "alpha(opacity=100)";
}else if(is_moz){
x=document.body.scrollLeft + document.getBoxObjectFor(obj).x;
y=document.body.scrollTop + document.getBoxObjectFor(obj).y + obj.clientHeight + 3;
$(id).style.opacity = "1.0";
}

try{
$(id).style.left = x+ "px";
$(id).style.top = y+"px";
}catch(e){}

try{
$(id+"_close").style.left = (parseInt(x) + parseInt(getClientWidth(obj)) -22)+ "px";
$(id+"_close").style.top = (parseInt(y)+2)+"px";
}catch(e){}
}
function openDiv(obj, id){
if( $(id) == null ){
createDiv(obj, id);
_showDiv(id, true);
_showDiv(id+"_close", true);
}
else{
_showDiv(id, true);
_showDiv(id+"_close", true);
}
fixSelectBug(false);
}

function _showDiv(id, isShow){
var x = $(id).style;
x.visibility = (isShow) ? 'visible':'hidden';
}
function _closeDiv(id){
_showDiv(id, false);
_showDiv(id+"_close", false);
fixSelectBug(true);
try{
this.afterClosed();//这里就我问的问题 } catch(exception){}
}
function setDivContext(id, context){
$(id).innerHTML = context;
}


var dhtmlTree_list = {};//树对象集合<Object{id 层编号,obj 表单对象,tree dhtmlTree对象(对象的属性方法请查阅官方文档)}>
dhtmlTree_list.size = 0;//集合大小
dhtmlTree_list.curIndex;//当前操作Object在集合中的下标
function getOrgEmp(obj){
var dhtmlTree = _getDhtmlTree(obj);

var div_id = dhtmlTree==null? new Date().getTime()+"_div" : dhtmlTree.id;
var tree_div_id = div_id+"_tree";
openDiv(obj, div_id);

if( $(tree_div_id) == null ){
setDivContext(div_id,"<div id='"+tree_div_id+"' style='OVERFLOW-x:auto;OVERFLOW-y:auto;width:"+getClientWidth(obj)+";height:"+getClientHeight(obj)+";'></div>");

var url = ajaxPath + "/OrgEmpTree.do";
var orgEmp_tree_obj=new dhtmlXTreeObject(tree_div_id,"100%","100%",0);
orgEmp_tree_obj.setImagePath(appPathTmp+"js/dhtmlxtree/codebase/imgs/csh_bluebooks/");
orgEmp_tree_obj.enableCheckBoxes(1);
orgEmp_tree_obj.enableThreeStateCheckboxes(true);
orgEmp_tree_obj.enableTreeImages(0);
orgEmp_tree_obj.setXMLAutoLoading(url);
orgEmp_tree_obj.loadXML(url);
orgEmp_tree_obj.attachEvent("onCheck",function(id, state){
if( state == "1" )
orgEmp_tree_obj.openAllItems(id);
else
orgEmp_tree_obj.closeAllItems(id);
});

var dhtmlTree = new Object();
dhtmlTree.id = div_id;
dhtmlTree.obj = obj;
dhtmlTree.tree = orgEmp_tree_obj;
dhtmlTree_list[dhtmlTree_list.size] = dhtmlTree;
dhtmlTree_list.curIndex = dhtmlTree_list.size;
dhtmlTree_list.size++;

try{
this.afterloaded();
} catch(exception){}
}
}
function _getDhtmlTree( obj ){
for(var i=0; i<dhtmlTree_list.size; i++){
if( dhtmlTree_list[i].obj === obj ){
dhtmlTree_list.curIndex = i;
return dhtmlTree_list[i];
}
}

return null;
}
chen32384778 2008-08-19
  • 打赏
  • 举报
回复
function b(){
this.b1();
}

直接调用b()而不是new对象的时候里面的this就是指的window对象了 就可以省略 即
function b(){
b1();
}
kofyf 2008-08-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chen32384778 的回复:]
//优雅不?
[/Quote]
这不是和try..catch..没什么差别呀?不过这也是一个思路。。
事件触发是否是这样实现的呢?
chen32384778 2008-08-19
  • 打赏
  • 举报
回复
<script>
function b(){
if(typeof b1 == 'function')
b1();
}
function b1(){
}
</script>
<a href="#" onclick="b();">test </a> <br>
//优雅不?
kofyf 2008-08-19
  • 打赏
  • 举报
回复
举个例子:
a.js文件
-----------
function a(){
//完成某些操作
...//在这里我想调用某空方法,不污染空间。所以不能直接写b();然后在a.js中定义一个空的b();可以理解为这里触发某事件或者调用某方法
}

aa.html文件
--------
<script src="a.js"></script>
<script>
//这里需要在a捕获在a()中触发的事件或者重载a()中这里调用的方法。完成某些功能
</script>
<a href="#" onclick="a();"></a>

我现在就是用try{}catch(){}来实现的。想请问有没有更好的实现方式。
kofyf 2008-08-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xingqiliudehuanghun 的回复:]
不太明白什么意思
[/Quote]
其实我是想实现如下功能。就是在b.prototype中定义个空方法。在b函数内部调用它。如果我在调用b()的页面想在这里做其他的处理。我就在调用页面重写b1()方法,实现某些功能。但是现在的情况是:如果我在b.prototype中定义一个方法而没有通过对象及:var temp = new b();temp.b1();这样调用而是在b1()方法内部调用不能实现。请问怎么实现我这样的思路?
  • 打赏
  • 举报
回复
不太明白什么意思
function b(){
this.b1=function(){
alert(222);
};
}
b.prototype.b1 = function(){
alert(1111);
};

var b=new b();
b.b1();
sxn19811006 2008-08-19
  • 打赏
  • 举报
回复
wo ye xiang zhi dao
fosjos 2008-08-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 kofyf 的帖子:]
但是感觉不是很优雅。
[/Quote]
前面的写法才是有问题的,不伦不类,要建对象就彻底些

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧