OOPs Concept In JavaScript
JavaScript is best known scripting language for web pages but it is also used in non browser environment. In contrast with Java, PHP, C++, Python etc uses OOPs concept. Main Property of OOPs is Object which work like real world object which have its specific properties and method. Object in JavaScript are flexible because of which new member can be added very easily in object with simple assignment. In JavaScript mainly three concepts of OOPs are use Polymorphism, Encapsulation and Inheritance.
Through inheritance special class can be created which is combination of more than one class. Special class will be child class with its own properties which inherits the property of its parent class. It allows reusability of generic code which makes the program simpler and smaller.
Encapsulation works like a capsule which contains properties of program like variable and methods. It combines all the functionalities of an object into another object so that all internal functionalities are hidden from rest of the world. Data hiding is key feature of Encapsulation; it works like shield for program. Those variables having access of Public only can be seen or modified as accordingly.
Polymorphism is property of Object Oriented Programming which has ability to call same method on different object and each has different response as according. It has key feature that we can add or override new member even after object has been created.
Example of JavaScript:
<! DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function person (first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
person.prototype.nationality = "English";
var myFather = new person ("John", "Doe", 50, "blue");
document.getElementById("demo").innerHTML =
"My father is " + myFather.nationality;
</script>
</body>
</html>
For more info : cpd-india
Batches: Regular & Weekends for Working Profession
For FREE DEMO CLASS CALL – 011-65164822 / 91- 8860352748
CPD Technologies
Block C 9/8, Sector – 7, Rohini, Delhi – 110085, India
Landmark: Near Rohini East Metro Station, Opposite Metro Pillar No-397
Website :- www.cpd-india.com
Email :- support@cpd-india.com