<html>
<head>
<script language="javascript">
<!--
function employee(name,code,designation){
this.name=name
this.code=code
this.designation=designation
}
newemp=new employee("John Dias","A001",'职员');
document.write("雇员姓名:"+newemp.name+"<br>");
document.write("雇员代号:"+newemp.code+"<br>");
document.write("头衔:"+newemp.designation);
</script>
</head></html>this在这里是不是指代下面新建的newemp对象,还是什么意思,好多程序中的代码就是这样一知半解的(自学java)
<head>
<script language="javascript">
<!--
function employee(name,code,designation){
this.name=name
this.code=code
this.designation=designation
}
newemp=new employee("John Dias","A001",'职员');
document.write("雇员姓名:"+newemp.name+"<br>");
document.write("雇员代号:"+newemp.code+"<br>");
document.write("头衔:"+newemp.designation);
</script>
</head></html>this在这里是不是指代下面新建的newemp对象,还是什么意思,好多程序中的代码就是这样一知半解的(自学java)
这里就是newemp
this.code=code
this.designation=designation this是当前的对象 就是第一行代码
employee的原始对象
就像java类那样
function employee(name, code, designation){
alert(this.name)//这里是undefined 说明这里是没有值的,
this.name = name//
alert(this.name)//这里把John Dias赋予了this.name
this.code = code
this.designation = designation
}
newemp = new employee("John Dias", "A001", '职员');
document.write("雇员姓名:" + newemp.name + " <br>");
document.write("雇员代号:" + newemp.code + " <br>");
document.write("头衔:" + newemp.designation);
this.name就是为了区分参数name与数据成员name的
我就说几个例子 :
public class Student{
private String name ;
priavte int age;
public Student()
public Student(String name ,int age)
{
this.name=name;// 当类的属性名和方法的局部变量名重名时用this来表示当前对象。哪么this.age 就表示当前对象的age属性。
this.age=name;
}
}
public class Student{
private String name ;
priavte int age;
public Student(String name)
{
this.Student(name,20);// 此处的this还可以调用本类中其他的构造方法。
}
public Student(String name ,int age)
{
this.name=name;// 当类的属性名和方法的局部变量名重名时用this来表示当前对象。哪么this.age 就表示当前对象的age属性。
this.age=name;
}
}
function employee(String n,String c,String d){
name=n;
code=c;
designation=d;
}
这么写你应该会看的清楚点,但是这么写的话程序不是很清楚明了!!!建议用this调用!!!
LZ你这个程序肯定通不过
function employee(name,code,designation){ /*这个方法的参数怎么没有类型?应该好匹配类型,应该这么写
this.name=name (String name,String code,String designation)
this.code=code */
this.designation=designation
}
看看这个,里面有关于关键字this的三大用法
http://blog.csdn.net/aa278489710/archive/2008/11/14/3298014.aspx