Looking under the hood in UnderscoreJS, I see:
我从敞篷下面看到:
_.isFunction = function(obj) {
return toString.call(obj) == '[object Function]';
};
_.isString = function(obj) {
return toString.call(obj) == '[object String]';
};
_.isNumber = function(obj) {
return toString.call(obj) == '[object Number]';
};
This seems like an odd choice. Why not just use typeof to determine whether a value is a string, function, or number? Is there a performance gain by using toString? Is typeof not supported by older browsers?
这似乎是个奇怪的选择。为什么不使用typeof来确定一个值是字符串、函数还是数字呢?使用toString是否有性能提升?旧浏览器不支持typeof吗?
2 个解决方案
#1
13
Well actually this is because it is faster to check the [[Class]]
by checking with toString
. Also there could be less mistakes, since toString gives you the exact Class ...
实际上,这是因为用toString检查[Class]会更快。另外,由于toString提供了确切的类,所以错误可能会更少。
check this :
检查:
var fn = function() {
console.log(typeof(arguments)) // returns object
console.log(arguments.toString()) // returns object Arguments
}
You could see the benchmark for underscore typeof vs toString here :
你可以在这里看到typeof和toString的基准:
http://jsperf.com/underscore-js-istype-alternatives
http://jsperf.com/underscore-js-istype-alternatives
Also there are some github issues with better explaination :
还有一些github上的问题需要更好的解释:
https://github.com/documentcloud/underscore/pull/332
https://github.com/documentcloud/underscore/pull/332
https://github.com/documentcloud/underscore/pull/321
https://github.com/documentcloud/underscore/pull/321
EDIT 1 :
编辑1:
You could also check this great article :
你也可以看看这篇伟大的文章:
http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
#2
2
drinchev's answer is partially correct. toString is currently much slower than using typeOf in most browsers. See the 7th revision of the test he posted which uses typeOf. Both are still very fast though so in most cases this performance difference won't be noticeable and the tradeoff is worth conforming to the specs better than duck typing / typeOf.
drinchev的答案是部分正确的。toString比大多数浏览器中的typeOf要慢得多。参见他发布的使用typeOf的测试的第七版。两者仍然是非常快的,但是在大多数情况下,性能差异不会被注意到,并且权衡比duck typing / typeOf更好地符合规范。
Underscore pull request 321 (that drinchev listed) has an in-depth discussion of the tradeoffs and why they decided to use toString.
下划线拉动请求321(德里内切夫列出的那个)深入讨论了权衡和为什么他们决定使用toString。
#1
13
Well actually this is because it is faster to check the [[Class]]
by checking with toString
. Also there could be less mistakes, since toString gives you the exact Class ...
实际上,这是因为用toString检查[Class]会更快。另外,由于toString提供了确切的类,所以错误可能会更少。
check this :
检查:
var fn = function() {
console.log(typeof(arguments)) // returns object
console.log(arguments.toString()) // returns object Arguments
}
You could see the benchmark for underscore typeof vs toString here :
你可以在这里看到typeof和toString的基准:
http://jsperf.com/underscore-js-istype-alternatives
http://jsperf.com/underscore-js-istype-alternatives
Also there are some github issues with better explaination :
还有一些github上的问题需要更好的解释:
https://github.com/documentcloud/underscore/pull/332
https://github.com/documentcloud/underscore/pull/332
https://github.com/documentcloud/underscore/pull/321
https://github.com/documentcloud/underscore/pull/321
EDIT 1 :
编辑1:
You could also check this great article :
你也可以看看这篇伟大的文章:
http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
#2
2
drinchev's answer is partially correct. toString is currently much slower than using typeOf in most browsers. See the 7th revision of the test he posted which uses typeOf. Both are still very fast though so in most cases this performance difference won't be noticeable and the tradeoff is worth conforming to the specs better than duck typing / typeOf.
drinchev的答案是部分正确的。toString比大多数浏览器中的typeOf要慢得多。参见他发布的使用typeOf的测试的第七版。两者仍然是非常快的,但是在大多数情况下,性能差异不会被注意到,并且权衡比duck typing / typeOf更好地符合规范。
Underscore pull request 321 (that drinchev listed) has an in-depth discussion of the tradeoffs and why they decided to use toString.
下划线拉动请求321(德里内切夫列出的那个)深入讨论了权衡和为什么他们决定使用toString。