-1

So, I've never done this before, trying to add a method to the Array.prototype. See console.log statement below for usage. It keeps telling me the method is not defined. I can't figure out what I'm doing wrong. Help!

My best conclusion / guess so far is that "this" is referring to the global object, and that's screwing it up somehow. But how to fix that, no clue. :(

const solution = input =>{
  
  Object.defineProperty(
      Array.prototype, 'polyReverse', {
        value: () => this ? (polyReverse(this.substr(1)) + this[0]) : (this),
        configurable: true, writable: true  
      }
  );

  console.log("string".split("").polyReverse().join(""));

};
/*****
 * 
 * ReferenceError: polyReverse is not defined
 *   at Array.value (main.js on line 4:20)
 * 
 * 
 *///////

NOTE: I also tried this for the value of value..

value: () => this ? (this.substr(1).polyReverse() + this[0]) : (this),

and this ...

value: () => this ? (this.polyReverse(this.substr(1)) + this[0]) : (this),

with no luck

Brian Patterson
  • 1,615
  • 2
  • 15
  • 31
  • 3
    Don’t use an arrow function here – Daniel A. White May 06 '22 at 22:13
  • 1
    Well, I see no definition of `polyReverse` in the code you've shared... – Heretic Monkey May 06 '22 at 22:13
  • 3
    Also, [Why is extending native objects a bad practice?](https://stackoverflow.com/q/14034180/215552) – Heretic Monkey May 06 '22 at 22:15
  • 1
    An arrow function for a method will use the lexical value of `this` from your declaration enviroinment, NOT the object reference that came with the method call. Moral of the story - **methods should not be declared as arrow functions**. Arrow functions are not just a shortcut syntax, they also have different behavior for the `this` value and a few other differences related to function arguments. – jfriend00 May 06 '22 at 22:31

2 Answers2

1

I tried the following and was able to resolve the issue of not defined

Array.prototype.polyReverse = function(value) {
  return this ? (this.substr(1).polyReverse() + this[0]) : (this)
};

console.log("string".split("").polyReverse().join(""));

Beyond this your logic seems to have some issue and it is throwing an error. I'm not sure what you are trying to achieve with polyReverse, so you would be the best person to fix that logic.

Since you specifically asked about the not defined issue, the above snippet should solve your problem and help you proceed further to fixing the logic

Aneesh
  • 1,720
  • 4
  • 14
0

So based on advice from helpful folks here, I was able to fix the problem as everyone said, do not use arrow functions. YES! Also, took note of how extending native methods is BAD! And I will make a sub class to do this now.

Thanks all for help.

Working code ...

const solution = input =>{
  
  Object.defineProperty(
      Array.prototype, 'polyReverse', {
        value: function() {
          console.log(this); 
          return (this.length > 0 ? 
           (this.join("").substr(1).split("").polyReverse() + this[0]) : 
           (this)
          )},
        configurable: true, 
        writable: true  
      }
  );

  console.log("string".split("").polyReverse());

};
Brian Patterson
  • 1,615
  • 2
  • 15
  • 31