如何写自定义对象的方法

redv 2002-08-13 06:09:59
function setErrorNumber(num)
{
this.number=num;//这里这么写
switch(this.number)
{
case 0:this.brief="Session is empty.";break;
case 1:this.brief="数据库中没有您的信息。";break;
case 2:this.brief="您的密码与用户名不匹选。";break;
default:this.brief="未知的错误。";
}
}
function getErrorNumber()//得到对象中的this.number的值
{
return this.number;//这里这么写
}
function UserError()
{
//Methods
this.setErrorNumber;
this.getErrorNumber;
//Properties
this.number=0;
this.brief="";
}
...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
redv 2002-08-13
  • 打赏
  • 举报
回复
谢谢两位,原来我的对象里面的方法忘了赋值.
qiushuiwuhen 2002-08-13
  • 打赏
  • 举报
回复
<script>
function setErrorNumber(num)
{
this.number=num;//这里这么写
switch(this.number)
{
case 0:this.brief="Session is empty.";break;
case 1:this.brief="数据库中没有您的信息。";break;
case 2:this.brief="您的密码与用户名不匹选。";break;
default:this.brief="未知的错误。";
}
}
function getErrorNumber()//得到对象中的this.number的值
{
return this.number;//这里这么写
}

function getErrorBrief()//得到对象中的this.brief的值
{
return this.brief;//这里这么写
}
function UserError()
{
//Methods
this.setErrorNumber=setErrorNumber;
this.getErrorNumber=getErrorNumber;
this.getErrorBrief=getErrorBrief;
//Properties
this.number=0;
this.brief="";
}

var t=new UserError();
t.setErrorNumber(1)
alert(t.getErrorNumber()+":"+t.getErrorBrief())
</script>
seabell 2002-08-13
  • 打赏
  • 举报
回复
<script>
function setErrorNumber(num)
{
this.number=num;//这里这么写
switch(this.number)
{
case 0:this.brief="Session is empty.";break;
case 1:this.brief="数据库中没有您的信息。";break;
case 2:this.brief="您的密码与用户名不匹选。";break;
default:this.brief="未知的错误。";
}
}
function getErrorNumber(mybool)//得到对象中的this.number的值
{
if(mybool)
return this.number;//这里这么写
else
return this.brief;
}
function UserError()
{
//Methods
this.setErrorNumber(2);
//this.setErrorNumber(3);
this.number=this.getErrorNumber(true);
this.brief=this.getErrorNumber(false);
//Properties
alert(this.number);
alert(this.brief);
}
UserError();
</script>

87,994

社区成员

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

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