最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

comparison - Compare types in Javascript - Stack Overflow

programmeradmin0浏览0评论

I saw in Crockford's Book Javascript: The Good Parts that he does typeof parison like this:

return typeof a < typeof b ? -1 : 1;

I made my own tests and I think this is the "ordering" of the different types:

function < number < object or array < string < undefined

Is this how JS actually does the parison?

I saw in Crockford's Book Javascript: The Good Parts that he does typeof parison like this:

return typeof a < typeof b ? -1 : 1;

I made my own tests and I think this is the "ordering" of the different types:

function < number < object or array < string < undefined

Is this how JS actually does the parison?

Share Improve this question edited Dec 23, 2011 at 22:50 pedro asked Dec 23, 2011 at 17:39 pedropedro 731 silver badge6 bronze badges 6
  • 1 Why would you need to test if a type is "less than" another? What does that even mean? Besides typeof() returns a string, so its really a lexographic parison. – Chad Commented Dec 23, 2011 at 17:42
  • @Chad: Probably sorting values in an Array by type using Array.prototype.sort. Here's an example – user1106925 Commented Dec 23, 2011 at 17:56
  • @amnotiam Like I said its not going to sort based on some type precedence though, it will sort lexicographically on the type string. – Chad Commented Dec 23, 2011 at 17:58
  • @Chad: I guess I should have said "grouping by type via sorting by typeof" instead of mere sorting. I haven't read The Good Parts (and have no plan to) so I don't know what the context is. – user1106925 Commented Dec 23, 2011 at 18:03
  • @amnotiam I agree I think the context of this statement makes all the difference. Defining a grouping sort is the only thing I can think of for this... – Chad Commented Dec 23, 2011 at 18:05
 |  Show 1 more ment

2 Answers 2

Reset to default 4

The typeof operator returns a string. String are pared by its numeric value.

So, the < parison order would be:

type       charCode ("tfnosux".charCodeAt(i))   Example
boolean     98                                   true
function   102                                   Date
number     110                                   123
object     111                                   []
string     115                                   ""
undefined  117                                   undefined
xml        120                                   <x></x>

tfnosux are the first characters of the types. The charCodeAt method returns the numeric charCode of a character in JavaScript.

I have added an example of each type at the previous block. Most JavaScript developers know about the first types. The final type, xml, is less monly known, and can be obtained by using typeof on EX4.

Demo of typeof: http://jsfiddle/9G9zt/1/

It is not importance. typeof returns a string, and the parison operators work for strings, by performing a "a simple lexicographic ordering on sequences of code point value values".

Basically, if one string starts with the other, then that is the greater of the two, otherwise the first character position that differs between the two is pared.

See section 11.8.5 of the spec

发布评论

评论列表(0)

  1. 暂无评论