2012-02-28 1 views
-3

私が見た文字列オブジェクトをチェックするための最も単純な偽の証明(?)メソッド。多くの異なるオブジェクトタイプでテストされています。それを上回る可能性のあるパラメータや状況はありますか?ここでより良いifString関数を構築するために.valueOf()を使用できますか?

function isString(o){ 
    if(o == null || o == undefined){ 
     return false; 
    } 
    if(typeof(o) == 'string'){ 
     return true; 
    } 
    if(typeof(o) == 'object'&& typeof(o.valueOf) == 'function' && typeof(o.valueOf()) == 'string'){ 
     return true; 
    } 
    return false; 
} 

は、私が確認したものです:

<html> 
<body> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
<input id="el"> 
<script type="text/javascript"> 

function isString(o){ 
    if(o == null || o == undefined){ 
     return false; 
    } 
    if(typeof(o) == 'string'){ 
     return true; 
    } 
    if(typeof(o) == 'object'&& typeof(o.valueOf) == 'function' && typeof(o.valueOf()) == 'string'){ 
     return true; 
    } 
    return false; 
} 
function tellIfString(o){ 
    if(isString(o)){ 
     return "A string!<br />"; 
    }else{ 
     return "Not a string!<br />"; 
    } 
} 
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!"); 

</script> 

</body> 
</html> 

は特にnew String.valueOf() https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String

+7

あなたの質問は何ですか?これはブログではありません。しかし、この関数は '{valueOf:function(){return 'foo';}}'を文字列として誤って分類します。 toString.call(o)=== '[オブジェクト文字列]') ' –

+0

ここに質問がありますか、またはあなたが作成した関数を投稿したいだけでしたか? –

+0

私はこれを答えの質問に再加工することをお勧めします。ガイドラインに準拠していれば、情報の共有を促進します:http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer - 前に尋ねる前に:http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Kev

答えて

3

に関する部分は、私はそれを書くために、より賢明だと思います(参照:

function isString(o) 
    { return typeof(o) == 'string' || o instanceof String; } 

追加編集:これは絶対に安全ではありません。コメントのFelix Klingは、それがうまくいかない場合を指摘しています。 this pageに基づいて、私はこれを信じています:

function isString(o) 
    { return Object.prototype.toString.call(o) === '[object String]'; } 

は "もっと"完全です。 (私はあなたがまだObject.prototype.toStringと混乱させることによってそれを壊すことができることを発見しました、そしておそらく他の方法で、あなた自身のコードをそれほど信用できなければ、あなたは何もできません)。

+2

これはほとんどの場合動作しますが、これが失敗する可能性がある場合があります。 iframeから 'String'オブジェクトを取得するとします。 –

+2

@FelixKling:十分に公正。私は私の答えを更新しました。 – ruakh

関連する問題