1

Maybe this question have beend posted before what I just do not know how to search for it.

I'd like to know how can I create a method like .replace(), or .toString(). I mean, if I have a variable and I want to search if that variable have number or not, like to do this

var someVariable = "hello 34, how are you";

var containsIntsNumber = someVariable.SearchInteger();//being search integer my custom method

if(containsIntsNumber )
{
console.log("It does have integers");
}

How can I achieve this?

5 Answers 5

1

You can modify the prototype on the String object.

String.prototype.someFunction = function () {
    /* Your function body here; you can use
       this to access the string itself */
};
Sign up to request clarification or add additional context in comments.

Comments

1

You can add it to the string prototype.

String.prototype.SearchInteger = function(){
  //do stuff
}

the you can call it like this

var someVariable = "hello 34, how are you";

var containsIntsNumber = someVariable.SearchInteger();

Adding additional functions to prototypes can be a bit controversial in the JS community. Be warned that it will then show up when you enumerate over the properties of the variable, and it could theoretically be overwritten or used for a different purpose by an external library.

Comments

1

This can be achieved in few ways. Have a function that return boolean value or extend string prototype so that you can call this method directly on string variable.

This will check wheather string has a number.

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

However it is not recommended to augment native objects, so my suggestion would be just use a function.

function hasInteger(value){
    return /\d/.test(value);  
}

2 Comments

but why i should not use it?
Augmenting objects introduces possibility of method collisions with native methods or augmentation by other code as well as undesirable side effects. You can do it but you should have deep knowledge of the language and the problem you are trying to address.
1
if(!String.prototype.SearchInteger)
{
    Object.defineProperty(String.prototype, 'SearchInteger',
    {
       value: function()
       {
           // body of your function here
       },
       enumerable: false
    });
}

1 Comment

@EinerS probably means "safe enough".
0

You will have to extend the prototype of String in this case. As String is an inbuilt type, It is not recommended to extend their prototype, but you can still do if you fancy(but dont!)

easy example would be something like

String.prototype.SearchInteger = function () {
     return this.test(/^.*\d+.*$/g);
};

this should work, though I didn't test.

2 Comments

programmers.stackexchange.com/questions/104320/… This discussion explains it better than I could. Keep in mind, there is nothing in js to stop you from doing it, so if you want you can do it easily( a lot of people do)
its better to simply do something like searchIntegersInString(String){ // same code as above should work }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.