javascript创建对象模板

白卡pala 2015-09-01 08:03:08

function sayhello(alerttxt){
try {
this fullname=fullname;
alert(fullname+alerttxt);
} catch (err) {
alert(err.description);
}

}



function testPerson(){
try {

mybrother=new person("徐白白",24,"bule");
document.getElementById('name').innerHTML=mybrother.fullname;
document.getElementById('age').innerHTML=mybrother.age;
document.getElementById('eyecolor').innerHTML=mybrother.eyecolor;
document.getElementById('b4').innerHTML=mybrother.sayhello("这是我的名字!");
} catch (err) {
// handle errors here
alert(err.description);
}


}


function person(fullname,age,eyecolor)
{
this fullname=fullname;
this age=age;
this eyecolor=eyecolor;
this sayhello=sayhello;

}



上面是我的js代码,为什么我把他放到自己的页面里,页面里所有的function都不能用了,将他们删除了,页面上其他的function又好了。
求解为什么?
代码哪里出错了吗?
...全文
94 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
我叫小菜菜 2015-09-01
  • 打赏
  • 举报
回复
引用 3 楼 xuwangcheng14 的回复:
[quote=引用 2 楼 zhi_ai_yaya 的回复:]
想当然了,以为是写java呢!!! 晕死~~~[/quote] 看来还得顺便补一下Java语法,哥们你适合学英语
白卡pala 2015-09-01
  • 打赏
  • 举报
回复
引用 2 楼 zhi_ai_yaya 的回复:
“上面是我的js代码,为什么我把他放到自己的页面里,页面里所有的function都不能用了,将他们删除了,页面上其他的function又好了。”这显然是代码错误,再也不往下执行也不做任何提示(其实是有提示的,但是没注意到)。 好好看看javascript的语法吧,不要想当然,this fullname=fullname;当英语造句?
//专门用一个函数来初始化对象。 
function Student(name,age,sex)
{ 
    this._name=name; 
    this._age=age; 
    this._sex=sex; 
}
想当然了,以为是写java呢!!! 晕死~~~
我叫小菜菜 2015-09-01
  • 打赏
  • 举报
回复
“上面是我的js代码,为什么我把他放到自己的页面里,页面里所有的function都不能用了,将他们删除了,页面上其他的function又好了。”这显然是代码错误,再也不往下执行也不做任何提示(其实是有提示的,但是没注意到)。 好好看看javascript的语法吧,不要想当然,this fullname=fullname;当英语造句?
//专门用一个函数来初始化对象。 
function Student(name,age,sex)
{ 
    this._name=name; 
    this._age=age; 
    this._sex=sex; 
}
天际的海浪 2015-09-01
  • 打赏
  • 举报
回复
this后面是点(.),不是空格

function sayhello(alerttxt){
	try {
		 this.fullname=fullname;
	alert(fullname+alerttxt);
	} catch (err) {
		alert(err.description);
	}

}



function testPerson(){
	try {

		 	mybrother=new person("徐白白",24,"bule");
	   document.getElementById('name').innerHTML=mybrother.fullname;
	   document.getElementById('age').innerHTML=mybrother.age;
	   document.getElementById('eyecolor').innerHTML=mybrother.eyecolor;
	   document.getElementById('b4').innerHTML=mybrother.sayhello("这是我的名字!");
	} catch (err) {
		// handle errors here
		alert(err.description);
	}


}


function person(fullname,age,eyecolor)
{
	this.fullname=fullname;
	this.age=age;
	this.eyecolor=eyecolor;
	this.sayhello=sayhello;

}

87,838

社区成员

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

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