js 相关问题求解惑

2018-03-29 11:46:01 +08:00
 iamjjh
<!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

2256 次点击
所在节点    JavaScript
3 条回复
iamjjh
2018-03-29 11:51:26 +08:00
已解惑...
zhlssg
2018-03-29 11:51:28 +08:00
new 的时候会把 this 绑定到实例上,你删了那行代码,就找不到这个方法了
iamjjh
2018-03-29 11:53:07 +08:00
@zhlssg 谢谢,已解惑

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/442418

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX