Created
          January 7, 2017 20:34 
        
      - 
      
- 
        Save Mordorreal/df25cb3358ab27e83c3f6ff382c2ec0f to your computer and use it in GitHub Desktop. 
    JS mimics array object 
  
        
  
    
      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 List { | |
| constructor(...args) { | |
| this.store = [...args]; | |
| this.length = this.store.length; | |
| this.createIndex(); | |
| } | |
| createIndex() { | |
| for (let i in this.store) { | |
| this[i] = this.store[i]; | |
| } | |
| } | |
| push = (...el) => { | |
| this.store.push(...el); | |
| this.length = this.store.length; | |
| this.createIndex(); | |
| return this.length; | |
| } | |
| pop = () => { | |
| const el = this.store.pop(); | |
| this.length = this.store.length; | |
| this[this.length] = undefined; | |
| return el; | |
| } | |
| shift = () => { | |
| const el = this.store.shift(); | |
| this.length = this.store.length; | |
| this[this.length] = undefined; | |
| this.createIndex(); | |
| return el; | |
| } | |
| unshift = (...el) => { | |
| this.store.unshift(...el); | |
| this.length = this.store.length; | |
| this.createIndex(); | |
| return this.length; | |
| } | |
| filter = (func) => { | |
| return this.store.filter(func); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment