Skip to content

Instantly share code, notes, and snippets.

View dlucidone's full-sized avatar

Ravi dlucidone

View GitHub Profile
Observer Design Pattern
1-https://pawelgrzybek.com/the-observer-pattern-in-javascript-explained/
JS-
let observers = [],
data;
const subscribe = f => observers.push(f);
const unsubscribe = f => observers = observers.filter(o => o !== f);
const notify = () => observers.forEach(o => o(data));
@dlucidone
dlucidone / Observer-Pattern.html
Last active January 15, 2019 04:54
JS BinObserver-Pattern// source https://jsbin.com/ferarux
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Observer-Pattern">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input class="js-input" type="text" placeholder="type something "/>
@dlucidone
dlucidone / index.html
Created January 15, 2019 06:46
JS Bin PubSub // source https://jsbin.com/jamevox
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="PubSub">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
@dlucidone
dlucidone / PromiseSimple.js
Created January 18, 2019 04:36
Promise Implementation
class PromiseSimple {
constructor(executionFunction) {
this.promiseChain = [];
this.handleError = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
executionFunction(this.onResolve, this.onReject);
}
@dlucidone
dlucidone / extend.js
Created January 22, 2019 03:30
Class Extend Implementation
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if (superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
@dlucidone
dlucidone / linkedlist.js
Last active February 6, 2019 05:19
Linked list JS
function Node(data){
this.data = data;
this.next = null;
}
function LinkedList(){
this.length=0;
this.head = null;
}
@dlucidone
dlucidone / apply.js
Created February 6, 2019 16:32
Apply Implementation
Function.prototype.myApply = function(){
if(arguments[0]==null||arguments[0]==this){
return this.bind(...arguments[1])();
}
else{
return this.bind(...arguments[0])();
}
}
@dlucidone
dlucidone / extend.js
Created February 8, 2019 03:59
extend implement
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
@dlucidone
dlucidone / bind.js
Last active February 8, 2019 04:31
JS Bind Implementation
Function.prototype.bind1 = function (scope) {
let fn = this
let prefixArgs = Array.prototype.slice.call(arguments, 1)
return function() {
let suffixArgs = Array.prototype.slice.call(arguments)
let args = prefixArgs.concat(suffixArgs)
return fn.apply(scope, args)
}
}
@dlucidone
dlucidone / call.js
Last active February 8, 2019 04:35
JS Bind Implementation
Function.prototype.myCall = function(){
return this.bind(...arguments)();
}
ex-
function showProfileMessage(message) {
console.log(message, this.name);
}
const obj = {
name: "Ankur Anand"