How To Define Properties In Javascript
13 October 2015
Hello everybody,
here is short notice of how to define properties in javascript:
So, lets say you want to have class Dog, with "private" field alias and public property Alias. you can achieve it in the following way:
function Dog(nameOfDog) {
var alias = nameOfDog;
Object.defineProperty(this, "Alias", {
get: function() {
return alias;
},
set: function(value) {
alias = value;
}
}
});
}
Later in code you can write the following:
var rex = new Dog("Rex");
var name = rex.Name; // name will be equal to "Rex"
Also you can set name of the dog to other name.