0

Newbie here! I was trying to write a code that returns true for digits ranging from 0 - 9 ONLY. Found this working code:

String.prototype.digit = function() {
   return /^\d$/.test(this);
};

Could someone explain what "this" mean in this scenario? And how do we decide to use it? I know it's related to function constructor, but I can't quite wrap my head around the concept.

Wyrd00
  • 1
  • 1
  • 1
    `this` refers to the object that comes before the `.` when you call the function like `"0".digit()`. – 4castle Apr 18 '17 at 03:16
  • When within a method `this` is the Object to which the method belongs. In this case `digit` is a method of `String.prototype`. – StackSlave Apr 18 '17 at 03:38

0 Answers0