I've been learning about method chaining and how, when a method returns this, the methods become chainable as each methods returns the object.
I'm wondering, how are Array methods (such as .map, .filter, .reduce, etc.) chainable, when they don't return this?
I'm guessing it has to do with the fact that Array.map() is in fact Array.prototype.map() and a new array, returned from Array.prototype.map(), has all of the Array methods on it's prototype, allowing it to be chainable?
How does this work under the hood? Does the Array.prototype.map() return a new array, with it's prototype set to this?
What I'd really like to learn, is how you could write an object that has methods, that return new arrays for example, that are chainable, without returning this? The same way that Array works.
This doesn't work but illustrates what I am thinking.
const generate = {
  method1(arr) {
    console.log(`Param: ${arr}`)
    const result = ['NameA', 'NameB']
    result.prototype = this
    return result
  },
  method2(arr) {
    console.log(`Param: ${arr}`)
    const result = ['NameC', 'NameD']
    result.prototype = this
    return result
  },
}
const example = generate.method1(['Paul']).method2(['Steve'])
console.log(example)
console.log(generate)
Another Attempt
const generate = {
  method1: (arr) => {
    console.log(`Param: ${arr}`)
    const result = ['NameA', 'NameB']
    return Object.assign(result, Object.create(this))
  },
  method2: (arr) => {
    console.log(`Param: ${arr}`)
    const result = ['NameC', 'NameD']
    return Object.assign(result, Object.create(this))
  },
}
const example = generate.method1(['Paul']).method2(['Steve'])
console.log(example)
console.log(generate)
 
     
    