1

I think I'm using the wrong terminology but here is what I would like to do. Using a function like this one:

function isNumeric(){
    if (isNaN(this)) { return false; }
    var x = parseFloat(this);
    return (x | 0) === x;
}; 

I know this function won't work as is. I removed the parameter that was originally passed in and replaced it inside the function with this. I would like to call it like so:

var tmp1 = 10;
var tmp2 = "10";

if( tmp1.isNumeric() == true && tmp2.isNumeric() == true ){
    ...
}

Instead of this:

if( isNumeric(tmp1) == true && isNumeric(tmp2) == true ){
    ...
}
1

4 Answers 4

5

The way to achieve that is not considered a good option, but it's to modify the prototype chain for the types of data you want your function to work with, e.g. for number and string like your example you could have:

Number.prototype.isNumeric = String.prototype.isNumeric = function() {
    // ...
}

What you have currently is the preferred option because it won't contaminate the prototype chain for inbuilt types, risk conflicts with other libraries, potentially overwrite functionality you didn't know existed, etc. You could meet halfway with something like:

class Val {
    constructor(value) {
        this.value = value;
    }

    isNumeric() {
        if (isNaN(this.value)) { return false; }
        var x = parseFloat(this.value);
        return (x | 0) === x;
    }
}

Used like:

var tmp1 = new Val(10);
var tmp2 = new Val('10');

console.log(tmp1.isNumeric(), tmp1.isNumeric());
Sign up to request clarification or add additional context in comments.

5 Comments

Besides contaminating the prototype chain is this a bad idea because it slows the performance down?
@DavidK I would say performance is way, way down the list of concerns.
I'm new to Javascript. Could you elaborate on other concerns?
@DavidK If you include any other JavaScript in your app that also tries to allocate a function isNumeric to the prototype chain of inbuilt types, that will get overwritten by your version and potentially break the third party code that was included originally because their implementation could differ significantly. This is very hard to debug, especially if the opposite happens where your isNumeric never seems to give the results you expect based on your implementation.
This is more of a concern for larger teams; it's very easy if your team is playing with the protoype of inbuilt types to define duplicate implementations of functionality that the application might need without realizing, causing debugging nightmares along with generally wasting time and creating poor code overall.
1

try to add this function to Object.prototype

Object.prototype.isNumeric = function () {
    return parseFloat(this) == this;
};

Comments

0

Below may be a better option for the class function if you are wanting "10" to return that is it not a number.

isNumeric() {    
  return typeof this.value === 'number';        
}

2 Comments

You misunderstood my function. 10,"10", 5e4, and so on all all valid numbers, they are numeric and can be used for math if handled properly.
Thank you for the clarification.
-1

isNumeric is just a function - what you are looking for is an object method. Right now, tmp1 and tmp2 are a Number, and String respectively, and neither of those have a function called isNumeric. You can restructure those variables like this if you want:

function TmpValue (initValue) {
  this.value = initValue
}

TmpValue.prototype.isNumeric = function() {
  if (isNaN(this.value)) { return false; }
  var x = parseFloat(this.value);
  return (x | 0) === x;
}

var tmp1 = new TmpValue(10);
var tmp2 = new TmpValue('10');

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.