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
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)); |
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
<!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 "/> |
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
<!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> |
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
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); | |
} |
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
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; | |
} |
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
function Node(data){ | |
this.data = data; | |
this.next = null; | |
} | |
function LinkedList(){ | |
this.length=0; | |
this.head = null; | |
} |
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
Function.prototype.myApply = function(){ | |
if(arguments[0]==null||arguments[0]==this){ | |
return this.bind(...arguments[1])(); | |
} | |
else{ | |
return this.bind(...arguments[0])(); | |
} | |
} |
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
// Shape - superclass | |
function Shape() { | |
this.x = 0; | |
this.y = 0; | |
} | |
// superclass method | |
Shape.prototype.move = function(x, y) { | |
this.x += x; | |
this.y += y; |
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
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) | |
} | |
} |
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
Function.prototype.myCall = function(){ | |
return this.bind(...arguments)(); | |
} | |
ex- | |
function showProfileMessage(message) { | |
console.log(message, this.name); | |
} | |
const obj = { | |
name: "Ankur Anand" |
OlderNewer