Skip to content

Instantly share code, notes, and snippets.

@leonardofreitass
Last active June 9, 2018 16:56
Show Gist options
  • Save leonardofreitass/7c71dcc880af626e8c9384e76d6446a3 to your computer and use it in GitHub Desktop.
Save leonardofreitass/7c71dcc880af626e8c9384e76d6446a3 to your computer and use it in GitHub Desktop.
Person class wrote in JavaScript
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
@jebadira
Copy link

jebadira commented Jun 9, 2018

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment