<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>W3Cschool 教程(w3cschool.cn)</title>
</head>
<body>
<script>
function person(firstname,lastname,age,eyecolor){
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name){
this.lastname=name;
}
}
myMother=new person("Sally","Rally",48,"green");
myMother.changeName("Doe");
document.write(myMother.lastname);
</script>
</body>
</html>
请问上面的代码为什么删除this.changeName=changeName;
就会报错?
提示Uncaught TypeError: myMother.changeName is not a function
1
iamjjh OP 已解惑...
|
2
zhlssg 2018-03-29 11:51:28 +08:00
new 的时候会把 this 绑定到实例上,你删了那行代码,就找不到这个方法了
|