To make a variable inaccessible from the outside, you need to make this variable
Local.
That is, declare a variable inside
in the function how
var
Html
<html>
<body>
<script>
// describe the class
function Book()
{
// Name variable inaccessible from the outside
var Name = "";
// Price variable accessible from the outside
this.Price = "";
this.setName = function(name)
{
Name = name;
};
this.getName = function()
{
return Name;
};
}
// create an instance of the class
var book1 = new Book();
// Price
book1.Price = 10;
alert(book1.Price); // on the screen we will see 10
// set Name
book1.setName("Wizard of the Mediterranean");
alert(book1.getName()); // on the screen we will see "Wizard of the Mediterranean"
// Name
alert(book1.Name); // Error! book1.Name is undefined
</script>
</body>
</html>