推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
iamjjh

js 相关问题求解惑

  •  
  •   iamjjh · Mar 29, 2018 · 3030 views
    This topic created in 2996 days ago, the information mentioned may be changed or developed.
    <!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
       Mar 29, 2018
    已解惑...
    zhlssg
        2
    zhlssg  
       Mar 29, 2018
    new 的时候会把 this 绑定到实例上,你删了那行代码,就找不到这个方法了
    iamjjh
        3
    iamjjh  
    OP
       Mar 29, 2018
    @zhlssg 谢谢,已解惑
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4362 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 05:30 · PVG 13:30 · LAX 22:30 · JFK 01:30
    ♥ Do have faith in what you're doing.