object oriented programming: encapsulation in … · 2020-02-04 · encapsulation example const...
TRANSCRIPT
Phachaya ChaiwchanLecturer in Information Science
Suan Sunandha Rajabhat University
Bangkok , Thailand
OBJECT ORIENTED PROGRAMMING:
ENCAPSULATION IN JAVASCRIPT
ENCAPSULATION
Why Encapsulation?
Why Encapsulation?
Why Encapsulation?
ENCAPSULATION
1
12
3
ENCAPSULATION
ENCAPSULATION
EXAMPLEfunction Hedgehog () {
let speed = 10000; // this is private
this.name = 'Sonic';
this.zoom = function () {
// both name and speed are accessible from here
console.log(`${this.name} zooms with the speed of ${speed} miles per second!`);
}
}
const sonic = new Hedgehog();
sonic.zoom();
console.log(sonic.name) //valid value
console.log(sonic.speed) // undefined
ENCAPSULATION
ENCAPSULATION
EXAMPLE
const createCounter = () => {
// A variable defined in a factory or constructor function scope
// is private to that function.
let count = 0;return ({
// Any other functions defined in the same scope are privileged:
// These both have access to the private `count` variable
// defined anywhere in their scope chain (containing function
// scopes).
click: () => count += 1,
getCount: () => count.toLocaleString()
});
};
const counter = createCounter();
counter.click();
counter.click();
counter.click();
console.log(
counter.getCount()
);
ENCAPSULATION
EXAMPLE class Counter {
#count = 0
click () {
this.#count += 1;
}
getCount () {
return this.#count.toLocaleString()
}
}
const myCounter = new Counter();
myCounter.click();
myCounter.click();
myCounter.click();
console.log(
myCounter.getCount()
);
ENCAPSULATION
EXAMPLE
var person = function () {
var fullName = “Justin Bieber";
var reg = new RegExp(/\d+/);
return {
setFullName : function (newValue) {
if( reg.test(newValue) ) {
alert("Invalid Name");
}
else {
fullName = newValue;
}
},
getFullName : function () {
return fullName;
}
}; // end of the return
}(); // Note the '()', this means we're calling the function
// and assigning the *returned object,* instead of
// the *function itself* for the value of 'person.'
alert(person.getFullName()); // Justin Bieber
person.setFullName( "Jason Shapiro" );
alert(person.getFullName()); // Jim White
person.setFullName( 40 ); // Invalid Name; the name is not changed.
person.fullName = 40; // Doesn't affect the private fullName variable.
alert(person.getFullName()); // Jim White is printed again.
ENCAPSULATION
ENCAPSULATION
CASE STUDY: coding
<script>
class Student
{
constructor()
{
var name;
var marks;
}
getName()
{
return this.name;
}
setName(name)
{
this.name=name;
}
getMarks()
{
return this.marks;
}
setMarks(marks)
{
this.marks=marks;
}
}
var stud=new Student();
stud.setName(“Dada");
stud.setMarks(92);
document.writeln(stud.getName()+" "+stud.getMarks());
</script>
www.javatpoint.com
ENCAPSULATION
CASE STUDY: coding
www.javatpoint.com
Object.defineProperty(this,"marks",{get:function(){return s_marks;},set:function(s_marks){this.s_marks=s_marks;}
});
}var stud=new Student(“Dada",92);document.writeln(stud.name+" "+stud.marks);</script>
Object.defineProperty(this,"marks",{get:function(){return s_marks;},set:function(s_marks){this.s_marks=s_marks;}
});
}var stud=new Student(“Dada",92);document.writeln(stud.name+" "+stud.marks);</script>
ENCAPSULATION
Q & A