Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you use .valueOf () to build better ifString function?
    primarykey
    data
    text
    <p>Simplest fool proof(?) method to check for string object I've seen. Tested with many different objects types. Are there parameters/circumstances that might trip it up, and are there simper functions available?</p> <pre><code>function isString(o){ if(o == null || o == undefined){ return false; } if(typeof(o) == 'string'){ return true; } if(typeof(o) == 'object'&amp;&amp; typeof(o.valueOf) == 'function' &amp;&amp; typeof(o.valueOf()) == 'string'){ return true; } return false; } </code></pre> <p>Here's what I've check with:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"&gt;&lt;/script&gt; &lt;input id="el"&gt; &lt;script type="text/javascript"&gt; function isString(o){ if(o == null || o == undefined){ return false; } if(typeof(o) == 'string'){ return true; } if(typeof(o) == 'object'&amp;&amp; typeof(o.valueOf) == 'function' &amp;&amp; typeof(o.valueOf()) == 'string'){ return true; } return false; } function tellIfString(o){ if(isString(o)){ return "A string!&lt;br /&gt;"; }else{ return "Not a string!&lt;br /&gt;"; } } function CustomO(a){ this.a = a; } literal = 'string'; objectWrapped = new String('why do i feel different?'); stringNumber = '345'; number = 234; numberWrapped = new Number(3345); object = {}; array = []; bool = true; nan = 1*'sdf'; regex = /woop/; regex_o = new RegExp('asasd'); o_no_valueOf = {}; o_no_valueOf = delete(o_no_valueOf.valueOf); customO = new CustomO({}); document.write("literal Is "+tellIfString(literal)); document.write("objectWrapped string Is "+tellIfString(objectWrapped)); document.write("stringNumber Is "+tellIfString(stringNumber)); document.write("number Is "+tellIfString(number)); document.write("numberWrapped Is "+tellIfString(numberWrapped)); document.write("object Is "+tellIfString(object)); document.write("array Is "+tellIfString(array)); document.write("bool Is "+tellIfString(bool)); document.write("nan Is "+tellIfString(nan)); document.write("null Is "+tellIfString(null)); //document.write("notdefined Is "+tellIfString(notdefined));//don't need to check variables that haven't been defined, they error before they get to the function document.write("undefined Is "+tellIfString(undefined)); document.write("Math Is "+tellIfString(Math)); document.write("function Is "+tellIfString(function(){})); document.write("regex Is "+tellIfString(regex)); document.write("regexO Is "+tellIfString(regex_o)); document.write("o_no_valueOf Is "+tellIfString(regex_o)); document.write("customO Is "+tellIfString(customO)); document.write("jQuery return Is "+tellIfString($('#el'))); document.write("document Is "+tellIfString(document));//this case is important because it requires the typeof(o.valueOf) == 'function' document.write("window Is "+tellIfString(window)); document.write("document.cookie Is "+tellIfString(document.cookie)); document.write("window.open Is "+tellIfString(window.open)); document.write("window.location Is "+tellIfString(window.location)); document.write("All objects evaluated!"); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>See also(especially the parts about <code>new String</code> and <code>.valueOf()</code> <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String" rel="nofollow">https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload