JavaScript Beginners Tutorial 21 | OOP | Encapsulation with example

preview_player
Показать описание
------------ UI TESTING ------------

------------ API TESTING ------------

------------ MOBILE TESTING ------------

------------ CI | CD | DEVOPS ------------

------------ VERSION CONTROL SYSTEM ------------

------------ PERFORMANCE TESTING ------------

------------ PROGRAMMING ------------

------------ IDE ------------

------------ MAVEN ------------

------------ OTHERS ------------

Keep Learning,
Raghav

You can support my mission for education by sharing this knowledge and helping as many people as you can.
Рекомендации по теме
Комментарии
Автор

after i called the setter method, i can access all the data and even change the data...how is this encapsulation??

alexlui
Автор

I think you made a mistake. Nothing is encapsulated there. You can either use a closure or the new feature of using a private class feature.

// Closure
class Employee {
constructor(name, id, phoneNo) {
this.getEmpName = function () {
return name;
};

this.getEmpId = function () {
return id;
};

this.getEmpPhoneNo = function () {
return phoneNo;
};
}
}

let emp1 = new Employee('John', 1001, 9874511);
console.log(emp1.name); // undefined
console.log(emp1.id); // undefined
console.log(emp1.phoneNo); // undefined

// John
console.log(emp1.getEmpId()); // 1001
// 9874511


// Or Using a Private Field

class Employee {
#EmpName;
#EmpId;
#EmpPhoneNo;

setEmpDeatils(name, id, phoneNo) {
this.#EmpName = name;
this.#EmpId = id;
this.#EmpPhoneNo = phoneNo;
}

getEmpName() {
return this.#EmpName;
}

getEmpId() {
return this.#EmpId;
}

getEmpPhoneNo() {
return this.#EmpPhoneNo;
}
}

const emp1 = new Employee();
emp1.setEmpDeatils('John', 1001, 9874511);
console.log(emp1);
console.log(emp1.#EmpName); // Private field '#num' must be declared in an enclosing class
console.log(emp1.#EmpId); //Private field '#num' must be declared in an enclosing class
//Private field '#num' must be declared in an enclosing class
console.log(emp1.getEmpId()); //1001;
// John
//9874511

RC-zfhp
Автор

Raghav some doubts. Please correct me. With your emp1 object, i can direct access all your attributes so how it can be encapsulation.

sajidsaiyed
Автор

Hey, where did you declare the access specifier for those variables, name and id, should be initialized as #name, #id, so that they are made private and could be accessed only using the getter methods. Please correct that. The following code works.

class PrivateEngineer{

#name = 'default';
#id = 0;

setName(name, id){
this.#name =name;
this.#id = id;
}

getName(){
return this.#name;
}

getId(){
return this.#id;
}

}

// Encapsulation
let emp = new PrivateEngineer();
emp.setName('Bharath', 2005);
console.log(emp.getName());
console.log(emp.getId());

barryreddy
Автор

You can still access the api publicly. How is this encapsulation?

CaliburPANDAs
Автор

Can you give an example in a real time project to implement encapsulation, inheritance, polymorphism, abstractions

aarthi
Автор

I have to invoke funcion in constructor, do you know how to do it?

kacperkepinski
Автор

On doubt sir in javaScript interface is avalible?

CodeWavewithShahHussain
Автор

How did you set private data i can't see it here?

TheKidrock
Автор

can i also get data by using console.log(emp1.phoneNo)

mohitsharma-ptjd
Автор

above setEmpDetails() should be set name, id, phoneNo as if they are private (#name, #id, #phoneNo) or protected (_name, _id, _phoneNo) and after that this.name and this.id, this.phoneNo as like if private this.#name or protected this._name the rest follows.

TheKidrock