English 中文(简体)
这一职能为什么没有评价某一论点的数据类型?
原标题:Why does this function fail to evaluate the data-type of a given argument?
function isDataType(dataType, obj) {
    return Object.prototype.toString.call(obj) ==  [object  + dataType +  ] ;
}

var arr = [];

alert(isDataType("Array", arr)); // alerts  false  which is false

当我把肥料等同于一个阵列,使数据类型成为阵列时,它仍然说谎。 是否有办法解决这一问题? 感谢。

最佳回答

页: 1 [目标。 之后,你们的法典应该真实地评估。

但是,如果某一物体是特定类型,则你应利用实例查明。

问题回答

是的——在发现阿雷拉数据类型时不使用这种方法寻找数据类型。 相反使用<代码>Array<>/code>。

你们只需要另一个空间吗?

 [object   + dataType +  ] 
        ^-- a space here

正如其他人已经提到的那样,这并没有一种巨大的数据类型测试方法。

function is(type, obj) {
    var clas = Object.prototype.toString.call(obj).slice(8, -1);
    return obj !== undefined && obj !== null && clas === type;
}

is( String ,  test ); // true
is( String , new String( test )); // true
function whatsit(what){
    if(what== null) return String(what);
    what= what.constructor;
    var Rx=  /functions+([^(s]+)s*(/, 
    tem= String(what).match(Rx);
    if(tem) return tem[1];
    return String(what);
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签