V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
iamjjh
V2EX  ›  JavaScript

js 相关问题求解惑

  •  
  •   iamjjh · 2018-03-29 11:46:01 +08:00 · 2227 次点击
    这是一个创建于 2191 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <!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

    iamjjh
        1
    iamjjh  
    OP
       2018-03-29 11:51:26 +08:00
    已解惑...
    zhlssg
        2
    zhlssg  
       2018-03-29 11:51:28 +08:00
    new 的时候会把 this 绑定到实例上,你删了那行代码,就找不到这个方法了
    iamjjh
        3
    iamjjh  
    OP
       2018-03-29 11:53:07 +08:00
    @zhlssg 谢谢,已解惑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   957 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:45 · PVG 05:45 · LAX 14:45 · JFK 17:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.