Comments (9)
我也看了这个问题,确实是个很诡异的 bug。
问题的起源虽然是 IE8 下 typeof dom 节点会返回 'function',但是讨论着讨论着问题就变了。
有一个人写了这样的一个测试代码,这里贴上代码:
function isFunction(value) {
return typeof value == 'function';
}
var fnc = function() {};
var obj = { 'a': 'b' };
var num = 1;
var xml = window.ActiveXObject ? new ActiveXObject('Msxml2.DOMDocument.3.0') : {};
console.log('before JIT isFunction(fnc). Expected: true; Got:', isFunction(fnc));
console.log('before JIT isFunction(obj). Expected: false; Got:', isFunction(obj));
console.log('before JIT isFunction(num). Expected: false; Got:', isFunction(num));
console.log('before JIT isFunction(xml). Expected: false; Got:', isFunction(xml));
var count = 200;
while (count--) {
isFunction(fnc);
}
console.log('typeof xml', typeof xml);
console.log('after JIT isFunction(fnc). Expected: true; Got:', isFunction(fnc));
console.log('after JIT isFunction(obj). Expected: false; Got:', isFunction(obj));
console.log('after JIT isFunction(num). Expected: false; Got:', isFunction(num));
console.log('after JIT isFunction(xml). Expected: false; Got:', isFunction(xml));
在 IE11 下的 non-edge document modes 下,最后一个
after JIT isFunction(xml). Expected: false; Got: true
这个人说是一个 IE11 下的 JIT bug,既然是编译上的 bug,解释的方式也无法按照常理理解了,为了让最后一个打印结果也变成 false, 只用在代码后面加上个|| false
。
后来这个人也验证了 IE11 下的 IE8 compat 模式的 DOM element 也有这个问题
var div = document.createElement('div');
// JIT the function
var count = 200;
while (count--) {
_.isFunction(fn);
}
equal(_.isFunction(div), false);
如果不加上 || false 的话,这里的结果会是 true,加上的话,就会避免这个 JIT bug。
最后,没有验证过哈~ 我理解的是这样~
from underscore-analysis.
@a13821190779
是有历史原因的,具体可以参考以下几个链接
- jashkenas/underscore#2594
- jashkenas/underscore#1621
- https://stackoverflow.com/questions/38827169/underscore-js-why-does-isfunction-use-false
from underscore-analysis.
@hanzichi 看了你提供的连接,好像因为ie8和11下 typeof dom 节点 有时候会返回 'function'
但是如果返回'fcuntion'的话那么这个 || false
也就无用了啊?
我理解的对吗?
from underscore-analysis.
并且我开虚拟机测试了一下ie8,并没有重现这个问题啊,我的代码如下:
var obj = {}
var type = typeof obj
console.log(type === 'function');
console.log(type === 'object');
console.log('----------');
console.log((typeof obj) === 'function')
console.log((typeof obj) === 'object')
from underscore-analysis.
同问。 @hanzichi 看了你提供的连接,好像因为ie8和11下 typeof dom 节点 有时候会返回 'function'
但是如果返回'fcuntion'的话那么这个 || false 也就无用了啊?
我理解的对吗?
from underscore-analysis.
为何加个 || false 就能改变结果= = 不明白
from underscore-analysis.
@mqyqingfeng 666 大神!
from underscore-analysis.
@mqyqingfeng 还是没看明白加不加 || false对于最后的结果为什么会有影响?
我看jashkenas/underscore#1621danschumann 描述typeof obj的结果保存到变量里的情况下是正确的,加 || false是隐式的实现了这一点吗?
from underscore-analysis.
@delayk 有人认为这是一个代码编译上的问题,加上 || false 是避免这个编译 bug,而不是通过执行这段代码从而获得正确的结果……不过提出这个问题的人也没有去验证他的这个 bug 是否可以通过这种方式解决……
from underscore-analysis.
Related Issues (20)
- JavaScript 数组乱序 HOT 13
- 浅谈 underscore 内部方法 group 的设计原理 HOT 3
- 带注释的源码中var _ = function (obj) {....}中有个疑问 HOT 4
- 关于 bind 你可能需要了解的知识点以及使用场景 HOT 2
- bind 方法的兼容实现 HOT 1
- JavaScript 函数节流和函数去抖应用场景辨析 HOT 17
- underscore 函数去抖的实现 HOT 20
- underscore 函数节流的实现 HOT 8
- 从斐波那契数列求值优化谈 _.memoize 方法 HOT 6
- Function Functions 相关源码拾遗
- 浅谈 Web 中前后端模板引擎的使用 HOT 19
- Underscore _.template 方法使用详解 HOT 3
- Underscore 整体架构浅析 HOT 15
- 源码解读这半年 HOT 20
- 疑问:模块导出中,向后兼容老的require() API HOT 1
- 源码
- 你好请教个问题 HOT 1
- 你好...在谷歌浏览器中运行`Object.prototype.toString().call(abb)`会报错 //var abb = true HOT 2
- 關於underscore源碼中提到了25個扩展方法之疑問? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from underscore-analysis.