Skip to content

Instantly share code, notes, and snippets.

View nishanbajracharya's full-sized avatar

Nishan Bajracharya nishanbajracharya

View GitHub Profile
/**
* Takes an object and checks if all given keys exist in the object.
*
* @param {Object} object
* @param {String|Array} keys
*
* @returns Boolean
*/
export const hasKeys = (object, keys) => {
if (!(object instanceof Array) && object instanceof Object) {
@nishanbajracharya
nishanbajracharya / this.js
Created September 26, 2018 13:53
This is javascript
var hello = document.getElementById('hello');
function Student(name, dob, el) {
this.name = name;
this.dob = dob;
this.el = el;
this.getProfile = function() {
console.log('In Get Profile', this);
return this.name + ' is an idiot';
@nishanbajracharya
nishanbajracharya / this.js
Created September 26, 2018 13:57
Getting around this;
var hello = document.getElementById('hello');
function Student(name, dob, el) {
var that = this;
this.name = name;
this.dob = dob;
this.el = el;
this.getProfile = function() {
@nishanbajracharya
nishanbajracharya / Observer.js
Created October 17, 2018 12:59
Javascript Observer Pattern
class Observer {
constructor() {
this.observers = [];
}
subscribe(fn) {
this.observers.push(fn);
return () => this.observers = this.observers.filter(subscriber => subscriber !== fn);
}
@nishanbajracharya
nishanbajracharya / roman.js
Last active October 29, 2018 05:32
Convert roman to integer
const ROMAN = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
class Subject {
constructor(state) {
this.state = state;
this.observers = [];
}
getState() {
return this.state;
}
class Observer {
constructor(subject) {
subject.registerObserver(this);
this.subscribers = [];
}
subscribe(subscriber) {
this.subscribers.push(subscriber);
}
const observable = new Subject(10);
const observerA = new Observer(observable);
const observerB = new Observer(observable);
observerA.subscribe(data => {
console.log("observerA", data);
});
observerB.subscribe(data => {
console.log("observerB", data);
@nishanbajracharya
nishanbajracharya / Observer.js
Last active June 11, 2020 08:31
Simple observer pattern with example
class Observer {
constructor(state) {
this.state = state;
this.subscribers = [];
}
get() {
return this.state;
}
const animate = document.getElementById('animate');
const btn = document.getElementById('btn');
let enabled = false;
btn.onclick = function() {
if (enabled) {
enabled = false;
transition(animate, {