简体   繁体   中英

not able to acces private method through privilaged methods in javascript

Error : undefined error in console, while trying to print age function

 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> 

What is this.age ? You did not encapsulate it correctly:

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

Note that you don't have to use var age = myage; . DEMO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM