开发者

Determing the type of the return value

I get a return value from a function which is always string. i used typeof(obj) which turns out to be string always.

Date.functionName=Date.prototype.functionName=function(dataObj) {
        //The dataObj is always string. 
            // How can i differentiate between the values... 
};

I have three things coming as strings..

  1. Date
  2. String...
  3. In开发者_Go百科teger values...

All the three are of type string.

  1. How can i differentiate these three values.
  2. Is it correct to use Date.mycustomfunction... or should i use some other class...


You can use isNaN to check if it's an integer or not. And there's some interesting ideas here for isDate type of functionality. Also some answers here may help: How can I determine whether a given string represents a date?


I was unsure about the date format used so you will need to add in the appropriate regular expression yourself:

var integer = null ;

if(   isNaN(  ( integer = parseInt(string) )  )  ===   false   ) ... //!! integer String values
else {

    if(  ( /*regular expression for your date format*/ ).test(string)  ===  true  ) //!! date String values
    else //!! all other String values

}

Btw, it is considered bad practice to add to native Objects in JavaScript because it is not as reliable as writing new Objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜