Created
April 12, 2020 03:52
-
-
Save jayjariwala/e0ba9b3ae3ed1fe44bdc2c3e31b484ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Object literals | |
const obj = { | |
radius: 1, | |
location: { | |
x : 1, | |
y : 2 | |
}, | |
draw:function () { | |
console.log('draw'); | |
} | |
} | |
// we can also define an object using factories and constructurs | |
//1) draw back of object literal syntax is that the object gets duplicate if it has a behaviour | |
// Factory Function | |
function createCircle(radius) { | |
return { | |
radius, | |
draw: function () { | |
console.log('Draw the circle'); | |
} | |
} | |
} | |
const circle = createCircle(3); | |
// Constrctor Function | |
function Circle(radius) { | |
this.radius = radius; | |
this.draw = function() { | |
console.log('draw'); | |
} | |
} | |
const another = new Circle(1); | |
// Constructor Property | |
// -> Every Object has a constrctor property and that reference the function that was used to create an object | |
// Functions are Objects | |
// -> check using Circle.__ we can access the members and methods | |
// Primitives are copied by value | |
// Reference types are copied by reference | |
console.log(another); | |
// Adding and removing properties | |
another.location = {x : 1}; | |
another['now'] = 10; | |
// iterate over object or propetry | |
for(let key in another) { | |
console.log(key , another[key]); | |
} | |
// iterate over only members and not methods | |
for(let key in another) { | |
if(typeof circle[key] !== 'function') { | |
console.log(key , another[key]); | |
} | |
} | |
// keys as an array | |
const key = Object.keys(another); | |
console.log(key); | |
/// Abstraction | |
// lets say if we have one object | |
function Ractangle(radius) { | |
this.height = 10; | |
this.width = 20; | |
this.radius = radius; | |
let defaultLocation = {x: 10, y:20}; | |
this.computeOptimumLocation = function() { | |
// here we are implementing a closure | |
console.log("The computed location is", defaultLocation.x + 10); | |
console.log("The computed location is", defaultLocation.y + 40); | |
} | |
this.draw = function() { | |
console.log("Draw the function with height::" +this.height+ "width::" + this.width); | |
} | |
} | |
const ract = new Ractangle(10); | |
ract.computeOptimumLocation(); | |
// Getter and Setters | |
function Triangle(radius) { | |
this.height = 10; | |
this.width = 20; | |
this.radius = radius; | |
let defaultLocation = {x: 10, y:20}; | |
this.computeOptimumLocation = function() { | |
// here we are implementing a closure | |
console.log("The computed location is", defaultLocation.x + 10); | |
console.log("The computed location is", defaultLocation.y + 40); | |
} | |
this.draw = function() { | |
console.log("Draw the function with height::" +this.height+ "width::" + this.width); | |
} | |
Object.defineProperty(this, 'defaultLocation', { | |
get: function() { | |
return defaultLocation; | |
}, | |
set: function (value) { | |
if(!value.x || !value.y) throw new Error("Invalid Location!"); | |
defaultLocation = value; | |
} | |
}) | |
} | |
const tri = new Triangle(10); | |
console.log("This is a default location::" ,tri.defaultLocation); | |
tri.defaultLocation = {x : null , y: null}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment