继承对象的内部function怎么调用? 本帖最后由 blood2323 于 2012-12-20 12:38:03 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不是很太明白你的意思? function initTopBar(){} initTopBar.prototype={ init:function(id,name,age){ this.id = id; this.name = name; this.age = age; //这里怎么调用print来打印内容啊 return print();//这样吗? }, print:function(id,name,age){ return console.log("编号:"+id+";姓名:"+name+";年龄:"+age); } } var p = new initTopBar(); window.onload = p.init(11,22,33); 差不多,但是return print()肯定不行的呀,就是用print的方法输出init的内容 设置成initTopBar的静态方法function initTopBar(){};initTopBar.prototype={ init:function(id,name,age){ this.id = id; this.name = name; this.age = age; //这里怎么调用print来打印内容啊 }, print:function(id,name,age){ return console.log("编号:"+id+";姓名:"+name+";年龄:"+age); },}initTopBar.print = function(id,name,age) { return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);};initTopBar.print(11,22,33);var p = new initTopBar();window.onload = p.init(11,22,33); 组件写法错误,修改如下:function initTopBar(id,name,age){ this.init(id,name,age);};initTopBar.prototype={ init:function(id,name,age){ this.id = id; this.name = name; this.age = age; //这里怎么调用print来打印内容啊 }, print:function(id,name,age){ return console.log("编号:"+id+";姓名:"+name+";年龄:"+age); },}window.onload = function(){ var p = new initTopBar(); p.print();} function initTopBar(id,name,age){ this.init(id,name,age);};initTopBar.prototype={ init:function(id,name,age){ this.id = id; this.name = name; this.age = age; //这里怎么调用print来打印内容啊 }, print:function(){ return console.log("编号:"+this.id+";姓名:"+this.name+";年龄:"+this.age); },}window.onload = function(){ var p = new initTopBar(); p.print();} function initTopBar(){};initTopBar.prototype={ init:function(id,name,age){ this.id = id; this.name = name; this.age = age; return (function(_id,_name,_age){ this.print(_id,_name,_age); }).apply(this,arguments); //这里怎么调用print来打印内容啊 }, print:function(id,name,age){ return console.log("编号:"+id+";姓名:"+name+";年龄:"+age); },}var p = new initTopBar();window.onload = p.init(11,22,33);这样是你要的效果吗? 不用设置成静态方法也可以,你返回一个匿名函数就可以了。函数函数里面调用print方法。完整代码在7楼。 return (function(_id,_name,_age){ this.print(_id,_name,_age); }).apply(this,arguments); return (function(_id,_name,_age){ this.print(_id,_name,_age); }).apply(this,arguments);这个能不能写成通用的,也许别的地方也会经常用到 还有一种情况呀,我不是要return的,只是再内部调用一下,这种情况怎么写?大神 内部调用直接调用,this.print(),把参数填上就行了。 太感谢了,终于对这个有点了解了,然后我又遇到一个问题了,大神帮我看看怎么解决<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><div id="test"></div><button id="123123"/></body></html><script>var Class={ create:function(){ return function(){ this.inital.apply(this,arguments); }; }};/*定义Person类*/var Person=Class.create();Person.prototype={ inital:function(id,name,age){ this.id = id; this.name = name; this.age = age; //this.getInfo(); }, getInformation:function(){ return console.log("编号:"+this.id+";姓名:"+this.name+";年龄:"+this.age) }, getInfo:function(){ this.getInformation(); }};var p = new Person(11,22,33);p.getInfo();//就是这里,如果上面这么写就是对的,然后放到事件里面就报错了,说 has no method 'getInformation'这是怎么回事情?!!!document.getElementById("123123").addEventListener('mouseover',p.getInfo, false);</script> document.getElementById("123123").addEventListener('mouseover',p.getInfo, false);改成document.getElementById("123123").addEventListener('mouseover',function(){ p.getInfo();}, false);这个就是this的问题了,你那种写法,this指向的是button对象。还有,不要叫我大神,我只是个菜鸟,不是什么大神。有问题直接问就行了。知道的我会回答。 怎么验证输入的密码是这样啊??? table 里面取指问题 想在这样的语句中传递参数,该如何做呢? 求助,怎么样在一个框架中取得另一个框架中的文件名? 散分求救!!! 怎样刷新第二个窗口? 请问怎样处理ActiveX控件的事件 怎样取得一个“字符串”所在的宽度?详情请进。。。。。。。 怎样加入插入图片的功能?? 请问怎么样知道一个页面是不是由showmodaldialog打开的 javascript 写个拼图并不容易 保证有解 关于ExtJs的TabPanel切换选项卡时,页面数据没有再次刷新
function initTopBar(){}
initTopBar.prototype={
init:function(id,name,age){
this.id = id;
this.name = name;
this.age = age;
//这里怎么调用print来打印内容啊
return print();//这样吗?
},
print:function(id,name,age){
return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
}
}
var p = new initTopBar();
window.onload = p.init(11,22,33);
initTopBar.prototype={
init:function(id,name,age){
this.id = id;
this.name = name;
this.age = age;
//这里怎么调用print来打印内容啊
},
print:function(id,name,age){
return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
},
}
initTopBar.print = function(id,name,age) {
return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
};
initTopBar.print(11,22,33);
var p = new initTopBar();
window.onload = p.init(11,22,33);
function initTopBar(id,name,age){
this.init(id,name,age);
};
initTopBar.prototype={
init:function(id,name,age){
this.id = id;
this.name = name;
this.age = age;
//这里怎么调用print来打印内容啊
},
print:function(id,name,age){
return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
},
}window.onload = function(){
var p = new initTopBar();
p.print();
}
this.init(id,name,age);
};
initTopBar.prototype={
init:function(id,name,age){
this.id = id;
this.name = name;
this.age = age;
//这里怎么调用print来打印内容啊
},
print:function(){
return console.log("编号:"+this.id+";姓名:"+this.name+";年龄:"+this.age);
},
}window.onload = function(){
var p = new initTopBar();
p.print();
}
function initTopBar(){};
initTopBar.prototype={
init:function(id,name,age){
this.id = id;
this.name = name;
this.age = age;
return (function(_id,_name,_age){
this.print(_id,_name,_age);
}).apply(this,arguments);
//这里怎么调用print来打印内容啊
},
print:function(id,name,age){
return console.log("编号:"+id+";姓名:"+name+";年龄:"+age);
},
}
var p = new initTopBar();
window.onload = p.init(11,22,33);
这样是你要的效果吗?
this.print(_id,_name,_age);
}).apply(this,arguments);
return (function(_id,_name,_age){
this.print(_id,_name,_age);
}).apply(this,arguments);这个能不能写成通用的,也许别的地方也会经常用到
太感谢了,终于对这个有点了解了,然后我又遇到一个问题了,大神帮我看看怎么解决
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<div id="test"></div>
<button id="123123"/>
</body>
</html>
<script>
var Class={
create:function(){
return function(){
this.inital.apply(this,arguments);
};
}
};
/*定义Person类*/
var Person=Class.create();
Person.prototype={
inital:function(id,name,age){
this.id = id;
this.name = name;
this.age = age;
//this.getInfo();
},
getInformation:function(){
return console.log("编号:"+this.id+";姓名:"+this.name+";年龄:"+this.age)
},
getInfo:function(){
this.getInformation();
}
};
var p = new Person(11,22,33);
p.getInfo();
//就是这里,如果上面这么写就是对的,然后放到事件里面就报错了,说 has no method 'getInformation'这是怎么回事情?!!!
document.getElementById("123123").addEventListener('mouseover',p.getInfo, false);</script>
document.getElementById("123123").addEventListener('mouseover',p.getInfo, false);改成document.getElementById("123123").addEventListener('mouseover',function(){
p.getInfo();
}, false);这个就是this的问题了,你那种写法,this指向的是button对象。
还有,不要叫我大神,我只是个菜鸟,不是什么大神。有问题直接问就行了。知道的我会回答。