2012-02-28 32 views
-3

最簡單的傻瓜證明(?)方法來檢查我見過的字符串對象。測試了許多不同的對象類型。是否有參數/情況可能會導致問題,並有可用的simper功能?您可以使用.valueOf()構建更好的ifString函數嗎?

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';}}'分類爲字符串。更好的可能是:if(({})。toString.call(o)==='[object String]')' – 2012-02-28 23:06:02

+0

這裏有個問題,或者你只是想發佈一個你創建的函數嗎? – 2012-02-28 23:10:04

+0

我會建議將這個問題改寫成帶有答案的問題。我們鼓勵您在遵守指南的情況下共享信息:http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer -before - 問也:http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ – Kev 2012-02-28 23:25:08

回答

3

我認爲這是更明智的寫:

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

編輯補充:這並非絕對萬無一失;菲利克斯克林在評論中指出了一個不起作用的例子。基於this page,我相信這個:

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

是「更」萬無一失。 (我發現你仍然可以通過與Object.prototype.toString混淆,也許以其他方式破壞它,但是如果你不太相信自己的代碼,那麼你確實沒有辦法做到。)

+2

儘管存在可能會失敗的邊緣情況,但大部分時間都可能有效。如果你從iframe中獲取一個String對象。 – 2012-02-28 23:16:16

+2

@FelixKling:夠公平的。我已經更新了我的答案。 – ruakh 2012-02-28 23:28:31

相關問題