0

混淆这个javascript

<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

myFather=new person("John","Doe",50,"blue");

document.write(myFather.firstname + " is " + myFather.age + " years old.");
</script>

约翰指的是 this.firstname 还是 firstname 在它之后?this.firstname 指的是什么?

4

2 回答 2

0

this.firstname 指的是引用对象的属性名。在您的情况下, myfather 是对象,因此 this.firstname 是指 myfather 的名字。

于 2013-10-01T13:47:00.840 回答
0

在 JavaScript 中,this 总是指我们正在执行的函数的“所有者”,或者更确切地说,指的是函数作为方法的对象。当我们在页面中定义我们忠实的函数 doSomething() 时,它的所有者就是页面,或者更确切地说,JavaScript 的窗口对象(或全局对象)

这里 this.firstname 指的是页面的所有者。

希望它会帮助你。

于 2013-10-01T13:47:11.580 回答