Here, through the function, created an object with a variable hidden from external access and a method for working with this variable:
function Machine() { var petrol = 100; this.getPetrol = function() { return petrol; }; this.setPetrol = function(value) { petrol = value; }; } var machine = new Machine(); var p = machine.getPetrol(); machine.setPetrol(5); var newP = machine.getPetrol(); console.log(p, newP); Now I would like to write the same object, but not through a function, but directly through an object. Please help me do this, here is my attempt:
var Machine = { this.petrol = 100, this.getPetrol = function() { return petrol; }, this.setPetrol = function(value) { petrol = value; } } var machine = new Machine(); machine.getPetrol();
petrolfield also be protected from external access? - ThisMan