最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Rename builtin prototype method in javascript - Stack Overflow

programmeradmin3浏览0评论

I am asked a question today that took me by surprise . I know string.repeat(number) repeat string to the said numbers in javascript. Example.

"Father".repeat(3)

Should print

FatherFatherFather

I was asked to do the same thing but instead using .repeat , i should use my new method like strRepeater in such a way that.

"Father".strRepeater(3)

Should equal

"Father".repeat(3);

Please how do i do this ? Any help would be appreciated.

I am asked a question today that took me by surprise . I know string.repeat(number) repeat string to the said numbers in javascript. Example.

"Father".repeat(3)

Should print

FatherFatherFather

I was asked to do the same thing but instead using .repeat , i should use my new method like strRepeater in such a way that.

"Father".strRepeater(3)

Should equal

"Father".repeat(3);

Please how do i do this ? Any help would be appreciated.

Share Improve this question edited Oct 26, 2016 at 8:10 Bergi 665k160 gold badges1k silver badges1.5k bronze badges asked Oct 26, 2016 at 1:44 Nuru SalihuNuru Salihu 4,92817 gold badges71 silver badges120 bronze badges 2
  • 1 Homework? Function.prototype.call will do the job just fine. – Knu Commented Oct 26, 2016 at 1:48
  • @Knu not home work. Interview actually and couldn't answer. So taken by surprise. Thank you though, would have a look. – Nuru Salihu Commented Oct 26, 2016 at 1:49
Add a comment  | 

3 Answers 3

Reset to default 14

There are 3 options:

  1. Creating an alias to the prototype:

    String.prototype.strRepeater = String.prototype.repeat;
    
  2. Creating a wrapper around the prototype:

    String.prototype.strRepeater = function() {
      return this.repeat.apply(this, arguments);
    };
    
  3. Creating your own method:

    String.prototype.strRepeater = function(times) {
      var res = "";
      for (var i = 0; i < times; i++) {
        res += this;
      }
      return res;
    };
    

While the other answers adding to the prototype are completely correct, they're also a bad habit to get into.

If adding anything to a prototype you should be using Object.defineProperty() so it doesn't appear as a member of the method (ie, a for...in loop will show up members, but not when added properly).

While this isn't a requirement for the String prototype, it's always a bad idea to get into bad habits and then wonder why things aren't working correctly later...

So the safe way to add the method is:

Object.defineProperty(String.prototype, "strRepeater", {
    value: function(number) {
        return this.repeat(number)
    }
};

Or to be even more safe:

if (!String.prototype["strRepeater"]) {
    Object.defineProperty(String.prototype, "strRepeater", {
        value: function(number) {
            return this.repeat(number)
        }
    };
}

On a technical note, this sets it with the defaults of enumerator: false, configurable: false and writeable: false - which translates to "no, you can't list me, delete me, or change me".

Object.defineProperty on MDN.

Try this:

String.prototype.strRepeater = function(number) {
  return this.repeat(number)
};

console.log("Father".strRepeater(3));

Explanations:

  • String.prototype.strRepeater add your function to the String object
  • this.repeat(number) will call the repeat built-in function with your current string inthis with number as param
  • return returns the result of .repeat() outside strRepeater()
发布评论

评论列表(0)

  1. 暂无评论