错误:尝试打印年龄函数时控制台中出现未定义的错误



var Person = function( myName, myProfession, myage ){
        this.name = myName;  // Public Variable
        this.profession = myProfession;
        var age = myage;  // Private Variable
        this.myAge = function(){  // Privilaged Method
            return this.age;
        };
        };
      var syed = new Person('syed azam','developer',20);
      console.log(syed + "works fine");
      console.log(syed.myAge());

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

最佳答案

什么是this.age?您没有正确封装它:

this.myAge = function(){
    return myage;
};


请注意,您不必使用var age = myage;DEMO.

10-04 17:55