javascript - 检测旧的 Internet Explorer Javascript 函数(< ES6)
问题描述
有网络在线、图书馆或其他东西来检测与 Chrome/Firefox 或仅 ES6 不兼容的旧 IE 功能?
比如:document.all、event.returnValue 等
JsHint/Jslint 没有将它们检测为已弃用或不兼容
解决方案
It's not quite fair to say JSLint won't tell you about deprecated properties. Let me explain.
Recall first that JavaScript is a dynamic language. You can assign any property to [almost] any object. You could assign all
to window
in a browser context if you wanted just by saying window.all = "Muahahaha!!! I'm evil!!!"
. You could add .all
to a string with...
var spam = "a string";
spam.all = "I'm still evil!!!"
Or, worse, some piece of code could have changed the prototype for String
(or any other object type) somewhere outside of your file. Try this in a browser console:
String.prototype.all = String.prototype.all || "This is beyond evil.";
// 'This is beyond evil.'
var spam = "spam"
// undefined
spam.all
// 'This is beyond evil.'
So JSLint doesn't, by default, check for properties on objects by names. Especially for objects that could live outside of your file's context (because JSLint lints file-by-file), it simply can't know what's happened to an object's properties and identify what's valid and what isn't.
(That's what TypeScript is for, btw.)
Unless you tell JSLint how!! -- the JSLint property
directive ftw
Or you can use the JSLint property
directive, which does exactly what you want, if you're willing to do some work.
If you put the property directive at the top of your file, JSLint will show errors for any properties that are used by objects on the page that aren't in that list.
For instance, try this on the official JSLint.com page:
/*property
log
*/
/*jslint browser, devel */
function mySpam() {
var spam = document.all;
console.log(spam);
}
See how I'm using document.all
but all
isn't in the property
directive? It's going to error for me.
1. Unregistered property name 'all'.
var spam = document.all;
You might be saying, "But it will take me FOREVER to get all the good properties from my 3000 line file I'm linting into that directive!!"
Not so! Here's a tip: Paste your file, even unlinted, into JSLint.com. It will create a property
directive for you in its report.
Here's one I made from AngularJS' [sic] route.js
in just a few seconds:
/*property
$$minErr, $evalAsync, $get, angularVersion, caseInsensitiveMatch, create,
defaultPrevented, eagerInstantiationEnabled, extend, info, isArray,
isDefined, isObject, isUndefined, length, module, noop, originalPath,
otherwise, preventDefault, provider, redirectTo, reload, reloadOnSearch,
reloadOnUrl, routes, run, substr, when
*/
Alphabetical, even.
Now just remove the ones you don't want and presto! You'll catch everything you need.
Is this a little tedious, and will it take a little massaging/training on files that use document
properly? Yes, but, again, in a dynamic language, this is close to the best you can hope for with file-by-file linters.
NOTE: If this doesn't solve your issue, however imperfectly, that's when we need to see more of your files and hear more precisely what problem you're trying to solve in practice.
推荐阅读
- powershell - 如何在 Windows 10 中保存 PowerShell 的状态以便以后继续使用?
- javascript - 递归解码 URL 给出有效 URL 的错误
- android - 如何在场景黑暗时自动打开手电筒进行视频录制?
- xml - xml 数据包含在标签内而不是外部
- flutter - 如何连接 Swift 文件和 dart 文件?
- argocd - argocd 的默认密码是多少?
- c# - 这个注释有什么不同吗[DataType(DataType.Text)]
- android - Android中的AES加密/解密
- java - 无法保存主要的 Jenkins 配置?
- angular - 通过使用 ng-packagr 向 ng-package.json 添加“资产”,全局样式未应用于 Angular 库