Tell us what’s happening:
I’m curious why when you use test() method, you use the the method as regex.test(str)
let regex = /Code/;
let result = regex.test(str);
When you use match() method, you use the method as str.match(regex)
let regex= /Code/;
let result = str.match(regex);
The only difference between the 2 methods is test() returns a boolean and match() returns a string.
I’m asking what is actually happening under the hood and if there was a higher level reasoning for this i.e. returning a string requires that match() takes regex as argument. Why?
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.
It is how they were implemented… test() is a regex method (RexExp.prototype.test()), match is a string method (String.prototype.match())
There is not a particular reason, with object oriented programming a method can be of one thing or of the other and the result is the same.
It should be asked to who implement the methods in the language
Well you mentioned that test() is a regex method and match() is a string method. This to me sounds like methods are classified into categories or types or whatever. I assume there are probably array methods and number methods.
So I was just curious how many types of methods there are. Are there less than a dozen? hundreds? thousands? Is there documentation where I can find this? Searching “Js methods” yields either a generic definition or some general description of a few different methods, but nothing that is encompassing.