Posts

Showing posts from October, 2017

Polymorphism in Java Script

Polymorphism The ability to call the same method on different objects and have each of them respond in their own way is called polymorphism. Polymorphism is the main pillar of Object Oriented Programming, it refers to the ability of an object to provide different behaviors in different state of objects. Example Parent class Human define a function of introduction() in which it contains simple introduction but Student object override it by adding more detailed information for introduction. So now Human() object has its own implementation of introduction and Student() has its own. It will be invoked according to the state of object. //Define human class function Human(fullName) {   this.fullName = fullName; } // Add a couple of methods to Human.prototype Human.prototype.speak = function(){   alert("I speak English!"); }; Human.prototype.introduction = function(){   alert("Hi, I am " + this.fullName); }; //Defi...

Object Oriented Java Script

With the basics out of the way, we'll now focus on object-oriented JavaScript (OOJS) — this article presents a basic view of object-oriented programming (OOP) theory, then explores how JavaScript emulates object classes via constructor functions, and how to create object instances. u Namespace u this keyword u Class u Object Namespace A namespace is a container which allows developers to bundle up functionality under a unique, application-specific name. In JavaScript a namespace is just another object containing methods, properties, and objects. The idea behind creating a namespace in JavaScript is simple: create one global object, and all variables, methods, and functions become properties of that object. Use of namespaces also reduces the chance of name conflicts in an application, since each application's objects are properties of an application-defined global object. Namespace we must have known from any language ...

Object Oriented Java Script Constructor Property Method

Object Oriented Programming (OOP) refers to using self-contained pieces of code to develop applications. We call these self-contained pieces of code objects, better known as Classes in most OOP programming languages and Functions in JavaScript. We use objects as building blocks for our applications. Building applications with objects allows us to adopt some valuable techniques, namely, Inheritance (objects can inherit features from other objects), Polymorphism (objects can share the same interface—how they are accessed and used—while their underlying implementation of the interface may differ), and Encapsulation (each object is responsible for specific tasks). Property Method Constructor Inheritance Encapsulation     Object Literals In this case you don't need to (and cannot) create an instance of the class, it already exists. So you simply start using this instance. apple.color = "reddish"; alert(apple.getInfo()); Object Con...

Encapsulation in Java Script

Encapsulation Encapsulation  refers to enclosing all the functionalities of an object within that object so that the object’s internal workings (its methods and properties) are hidden from the rest of the application. One of the main principles with OOP is encapsulation: put all the inner workings of an object inside that object. To implement encapsulation in JavaScript, we have to define the core methods and properties on that object. To do this, we will use the best pattern for encapsulation in JavaScript: the  Combination Constructor/Prototype Pattern . Implementation of Combination Constructor/Prototype Pattern Example:    function User (theName, theEmail) {     this.name = theName;     this.email = theEmail;     this.quizScores = [];     this.currentScore = 0; }           Implementation of Combinat...