JavaScript OOP
What is Object Oriented Programming?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Javascript is Different from Most Other OOP
- There is no class in Javascript
- Every component in JavaScript is an Object, including Functions, Strings, and Numbers.
- Uses object literals or constructor functions to create objects
- Uses prototype inheritance
Function in Javascript
In Javascript, functions double as constructors. You call a function as a constructor by using the new operator.
Javascript Prototype
Advantage of using the prototype object to add functions
- No matter how many objects you create, functions are loaded only once into memory
- Allows you to override function if required
Encapsulation
- Concept: enclosing all the functionalities of an object within that object.
- Then we can instantiate an instance of User using User constructor.
Inheritance
In JavaScript, we don't have the concept of class, so inheritance in JavaScript is prototype based. This means to implement inheritance in JavaScript, an object inherits from another object.
Polymorphism
The ability to redefine methods for derived classes.
JS PATTERNS
Homework
https://learn.javascript.ru/oop ООП в функциональном стиле
https://learn.javascript.ru/prototypes ООП в прототипном стиле
http://habrahabr.ru/post/144646 Понимание ООП в JavaScript
http://habrahabr.ru/post/144646 Javascript: ООП, прототипы, замыкания, «класс» Timer.js
http://tobyho.com/2010/11/22/javascript-constructors-and Javascript Constructors and Prototypes
https://raw.githubusercontent.com/coodict/javascript-in-one-pic/master/js%20in%20one%20pic.png All JavaScript ( taken from GitHub)
Classwork
- Create an object called shape that has the type property and a getType(), getPerimeter() and draw() method. Draw method shows in console that (*name is drawn)
- Define a Triangle() constructor function whose prototype is shape. Objects created with Triangle() should have three own properties—a, b, and c, representing the lengths of the sides of a triangle.
- Define a Square() constructor function whose prototype is shape. Objects created with Square() should have three own properties—a, b, c, and d representing the lengths of the sides of a square.
- Add one more Shape that inherits Triangle or Square or some other shape (in this case you should add it too) and has some specific method and properties
THANKS FOR YOUR ATTENTION
JAVASCRIPT OOP
Copy of JavaScript OOP
By ilyinalada
Copy of JavaScript OOP
- 345