Last active
March 18, 2021 13:24
-
-
Save skrajewski/d33bfee3a1b31d0bb7c8ea42bcf113a6 to your computer and use it in GitHub Desktop.
Class or function. What is better?
This file contains hidden or 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
const stackA = new Stack(); | |
stackA.push("abc"); | |
stackA.push("xyz"); | |
console.log(stackA.stack); | |
stackA.stack = []; | |
console.log(stackA.stack); | |
const stackB = createStack() | |
stackB.push("abc"); | |
stackB.push("xyz"); | |
console.log(stackB.stack); | |
stackB.stack = []; | |
console.log(stackB.stack); | |
console.log(stackB.pop()); | |
const stackC = createStack() | |
console.log(stackC.pop()); |
This file contains hidden or 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 Stack { | |
constructor() { | |
this.stack = []; | |
} | |
push(el) { | |
this.stack.push(el); | |
} | |
pop() { | |
return this.stack.pop(); | |
} | |
} | |
const createStack = function () { | |
const stack = []; | |
const push = (el) => stack.push(el); | |
const pop = () => stack.pop(); | |
return { | |
push, pop | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment