Difference between revisions of "Javascript Object Oriented Programming"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:Javascript Category:OOP == 4 Pillars of OOP == * Encapsulation * Abstraction * Inheritance * Polymorphism <pre class="brush:js;"> // The simples...")
 
Line 8: Line 8:
 
* [[Polymorphism]]
 
* [[Polymorphism]]
  
 +
== Cheat Sheet ==
 +
The simplest way to create an object is using an object literal
 
<pre class="brush:js;">
 
<pre class="brush:js;">
// The simplest way to create an object is using an object literal
 
 
const circle = {  
 
const circle = {  
 
   radius: 1,  
 
   radius: 1,  
 
   draw: function() {}
 
   draw: function() {}
 
};  
 
};  
 
+
</pre> 
// To create multiple objects with the same structure and behaviuor (methods), use a factory or a constructor.  
+
To create multiple objects with the same structure and behaviuor (methods), use a factory or a constructor.  
 
+
==== Factory function ====
// Factory function  
+
<pre class="brush:js;">
 
function createCircle(radius) {  
 
function createCircle(radius) {  
 
   return {
 
   return {
Line 24: Line 25:
 
   }  
 
   }  
 
}  
 
}  
 
+
</pre>
// Constructor function  
+
==== Constructor function ====
 +
<pre class="brush:js;">
 
function Circle(radius) {  
 
function Circle(radius) {  
 
     this.radius = radius;  
 
     this.radius = radius;  
 
     this.draw = function() {}
 
     this.draw = function() {}
 
}  
 
}  
   
+
</pre>   
// Every object has a "constructor" property which returns the function that was used to construct or create that object.  
+
Every object has a "constructor" property which returns the function that was used to construct or create that object.  
 +
<pre class="brush:js;">
 
const x = {};
 
const x = {};
 
x.constructor; // returns Object()  
 
x.constructor; // returns Object()  
 
+
</pre> 
// In JavaScript, functions are objects. They have properties and methods.  
+
In JavaScript, functions are objects. They have properties and methods.  
 +
<pre class="brush:js;">
 
Circle.name;  
 
Circle.name;  
 
Circle.length;
 
Circle.length;
Line 41: Line 45:
 
Circle.call({}, 1); // to call the Circle function  
 
Circle.call({}, 1); // to call the Circle function  
 
Circle.apply({}, [1]);
 
Circle.apply({}, [1]);
 +
</pre>
 +
Value types are copied by their value, reference types are copied by their reference.
  
// Value types are copied by their value, reference types are copied by their reference.
+
'''Value types''' in JavaScript are: String, Number, Boolean, Symbol, undefined and null
// Value types in JavaScript are: String, Number, Boolean, Symbol, undefined and null
+
 
// Reference types are: Object, Function and Array  
+
'''Reference types''' are: Object, Function and Array  
 
    
 
    
// JavaScript objects are dynamic. You can add/remove properties:  
+
 
 +
JavaScript objects are dynamic. You can add/remove properties:  
 +
<pre class="brush:js;">
 
circle.location = {};
 
circle.location = {};
 
circle['location'] = {};
 
circle['location'] = {};
 
                        
 
                        
 
delete circle.location;  
 
delete circle.location;  
                     
+
</pre>                     
// To enumerate the members in an object:  
+
To enumerate the members in an object:  
 +
<pre class="brush:js;">
 
for (let key in circle) console.log(key, circle[key]);
 
for (let key in circle) console.log(key, circle[key]);
  
 
Object.keys(circle);  
 
Object.keys(circle);  
                     
+
</pre>                     
// To see if an object has a given property
+
To see if an object has a given property
 +
<pre class="brush:js;">
 
if ('location' in circle)
 
if ('location' in circle)
                     
+
</pre>                     
// Abstraction means hiding the complexity/details and showing only the essentials.
+
// We can hide the details by using private members. Replace "this" with "let".
+
  
 +
Abstraction means hiding the complexity/details and showing only the essentials.
 +
 +
We can hide the details by using private members. '''Replace "this" with "let".'''
 +
<pre class="brush:js;">
 
function Circle(radius) {  
 
function Circle(radius) {  
 
   // Public member  
 
   // Public member  
Line 70: Line 82:
 
   let defaultLocation = {};                       
 
   let defaultLocation = {};                       
 
}                       
 
}                       
 
+
</pre>
// To define a getter/setter, use Object.defineProperty():
+
To define a getter/setter, use Object.defineProperty():
 
+
<pre class="brush:js;">
 
Object.defineProperty(this, 'defaultLocation', {
 
Object.defineProperty(this, 'defaultLocation', {
 
     get: function() { return defaultLocation; },
 
     get: function() { return defaultLocation; },

Revision as of 09:36, 28 August 2018


4 Pillars of OOP

Cheat Sheet

The simplest way to create an object is using an object literal

const circle = { 
   radius: 1, 
   draw: function() {}
}; 

To create multiple objects with the same structure and behaviuor (methods), use a factory or a constructor.

Factory function

function createCircle(radius) { 
   return {
      radius, 
      draw: function() {}
   } 
} 

Constructor function

function Circle(radius) { 
    this.radius = radius; 
    this.draw = function() {}
} 

Every object has a "constructor" property which returns the function that was used to construct or create that object.

const x = {};
x.constructor; // returns Object() 

In JavaScript, functions are objects. They have properties and methods.

Circle.name; 
Circle.length;
Circle.constructor; // returns Function()
Circle.call({}, 1); // to call the Circle function 
Circle.apply({}, [1]);

Value types are copied by their value, reference types are copied by their reference.

Value types in JavaScript are: String, Number, Boolean, Symbol, undefined and null

Reference types are: Object, Function and Array


JavaScript objects are dynamic. You can add/remove properties:

circle.location = {};
circle['location'] = {};
                      
delete circle.location; 

To enumerate the members in an object:

for (let key in circle) console.log(key, circle[key]);

Object.keys(circle); 

To see if an object has a given property

if ('location' in circle)

Abstraction means hiding the complexity/details and showing only the essentials.

We can hide the details by using private members. Replace "this" with "let".

function Circle(radius) { 
   // Public member 
   this.radius = radius; 

   // Private member                       
   let defaultLocation = {};                      
}                       

To define a getter/setter, use Object.defineProperty():

Object.defineProperty(this, 'defaultLocation', {
    get: function() { return defaultLocation; },
    set: function(value) { defaultLocation = value; }
});