+ All Categories
Home > Documents > OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const...

OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const...

Date post: 18-Jun-2020
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
17
Phachaya Chaiwchan Lecturer in Information Science Suan Sunandha Rajabhat University Bangkok , Thailand [email protected] OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN JAVASCRIPT
Transcript
Page 1: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

Phachaya ChaiwchanLecturer in Information Science

Suan Sunandha Rajabhat University

Bangkok , Thailand

[email protected]

OBJECT ORIENTED PROGRAMMING:

ENCAPSULATION IN JAVASCRIPT

Page 2: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

ENCAPSULATION

Page 3: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

Why Encapsulation?

Page 4: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

Why Encapsulation?

Page 5: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

Why Encapsulation?

Page 6: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

ENCAPSULATION

1

12

3

Page 7: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

ENCAPSULATION

Page 8: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

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

Page 9: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

ENCAPSULATION

Page 10: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

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()

);

Page 11: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

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()

);

Page 12: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

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.

Page 13: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

ENCAPSULATION

Page 14: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

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

Page 15: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

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>

Page 16: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

ENCAPSULATION

Page 17: OBJECT ORIENTED PROGRAMMING: ENCAPSULATION IN … · 2020-02-04 · ENCAPSULATION EXAMPLE const createCounter = => {// A variable defined in a factory or constructor function scope

Q & A


Recommended