Last active
June 9, 2018 16:56
-
-
Save leonardofreitass/7c71dcc880af626e8c9384e76d6446a3 to your computer and use it in GitHub Desktop.
Person class wrote in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
constructor(name, age){ | |
this.name = name; | |
this.age = age; | |
} | |
advanceAge(){ | |
this.age++; | |
} | |
} | |
function doBirthday(citizen){ | |
citizen.advanceAge(); | |
console.log(`Happy ${citize.age} years!`); | |
} | |
const leonardo = new Person('Leonardo', 21); | |
doBirthday(leonardo); | |
leonardo.age = 21; // Note that this is possible, there is no way to make a attribute private on JS | |
leonardo.age = 'twenty'; // This is possible as well, there is no type check |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how you set private variables.
function Foo(a) {
var bar = a;
this.getBar = function() {return(bar);}
this.setBar = function(a) {bar = a;}
}
var x = new Foo(3);
var y = x.getBar();
x.setBar(12);
var z = x.bar;