- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我今天发现这个来检查某些东西是否是 bool 值:
而不是:
typeof a === 'boolean' && //do sth
您可以使用:
a === !!a && //do sth
还有其他类似的方法来检查 typeof string/number/obj/arr
等吗?
我不是在寻找最佳实践,而是在寻找进行“类型检查”的巧妙方法。
最佳答案
javascript中的类型并不那么简单,而是 this对您来说是一个很好的指南!
检查 Javascript 中的类型是一团糟。
typeof operator
In the beginning, there was typeof. This handy operator gives you the "type" of a Javascript value:
typeof 3 // "number"
typeof "abc" // "string"
typeof {} // "object"
typeof true // "boolean"
typeof undefined // "undefined"
typeof function(){} // "function"
All is fine 'n dandy until
typeof [] // "object"
Huh? An array's type is object? I guess it is, if you want to get technical about it, but still, what the...
typeof null // "object"
Okay, now that's just wrong!
instanceof operator
The instanceof operator tells you whether a object is an instance of a certain type. The so-called "type" is a constructor. For example
function Animal(){}
var a = new Animal()
a instanceof Animal // true
Cross-window Issues of instanceof
It turns out that instanceof has another problem. It breaks down when you try to test an object coming from another window. You know? The ones that are created for each , or popup window that you create.
var iframe = document.createElement('iframe')
document.body.appendChild(iframe)
var iWindow = iframe.contentWindow // get a reference to the window object of the iframe
iWindow.document.write('<script>var arr = [1, 2, 3]</script>') // create an array var in iframe's window
iWindow.arr // [1, 2, 3]
iWindow.arr instanceof Array // false
Duck-Typing
Because neither typeof or instanceof are satisfactory, many resort to duck-typing. This means checking the behavior: if it looks like a duck and quacks like a duck, then it is a duck as far as I am concerned. Pretty sure I misquoted that...oh well.
So, using duck-typing, an isArray check might look like
// source: http://forums.devshed.com/javascript-development-115/javascript-test-whether-a-variable-is-array-or-not-33051.html
function isArray(obj){
return (typeof(obj.length)=="undefined") ?
false:true;
}
Object.prototype.toString method
It turns out that, you can get type information about an object by using the Object.prototype.toString method.
Object.prototype.toString.call(3) // "[object Number]"
Object.prototype.toString.call([1, 2, 3]) // "[object Array]"
Object.prototype.toString.call({}) // "[object Object]"
Function.prototype.toString method
Yet another way to test for type information is by using the Function.prototype.toString method.
Function.prototype.toString.call((3).constructor)
// "function Number() {
// [native code]
// }"
DOM Elements and Host Objects
So far, I have not mentioned type checking for DOM elements and host objects. That's because it's hard. With the exception of duck-typing, none of the methods mentioned above will work for all browsers. If you drop IE7 and below, however, you can actually get some of the things to work. The output below were created using Tutti
var div = document.createElement('div')
typeof div
Safari 5.0 => object
Firefox 3.6 => object
IE 7.0 => object
IE 8.0 => object
Opera 11.01 => object
div instanceof Element
Safari 5.0 => true
Firefox 3.6 => true
IE 7.0 => Error: 'Element' is undefined
IE 8.0 => true
Opera 11.01 => true
div instanceof HTMLDivElement
Safari 5.0 => true
Firefox 3.6 => true
IE 8.0 => true
IE 7.0 => Error: 'HTMLDivElement' is undefined
Opera 11.01 => true
关于javascript - 检查任何类型的 typeof 的最短方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55418312/
这个问题在这里已经有了答案: Integer summing blues, short += short problem (5 个答案) 关闭 7 年前。 版本:Visual Studio Prof
我尝试执行以下代码: public class Test5 { /** * @param args */ public static void main(String[] args) {
这是我的任务,我尝试仅使用简短的 if 语句来完成此任务,我得到的唯一错误是使用“(0.5<=ratio<2 )”,除此之外,构造正确吗? Scanner scn = new Scanner(
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有一个简单的类型 data Day = Monday | Tuesday | Wednesday | Thursday | Friday 我是haskell的新手,所以我写==如下。 (==) :
如何实现“简短”和“详细”两个按钮? “短”应该是默认值,并显示页面的一个版本。单击“详细”按钮后,应显示该页面的另一个版本。 由于这有点难以解释,或许可以看下面的例子。 示例页面: 别管内容 需要j
有没有一种方法可以在 C# 中执行此操作,而无需为现有的每个 var 类型创建一个新方法来重载? $box = !empty($toy) : $toy ? ""; 我能想到的唯一方法是: if (t
我想使用 setInterval 创建一个节拍器。我希望能够达到 300 bpm 这样的高 bpm。即使文件足够短,可以根据需要播放多次,它也很容易 打嗝。此外,许多浏览器都存在短音频文件的问题——S
我们现在有一个正在生产中的应用程序,它会将 IAP 收据发送到我们的服务器,这些收据显然太短,而且我们的服务器没有经过 apple 的验证。 Apple 正确验证的长收据长度为 3192。短收据长度均
例如,许多软件使用的许可证 key 。我曾想过对一个序列进行密码签名,所以我可能有 4 个字节用于 ID,8 个字节用于签名,但我找不到合适的算法。 我需要的是攻击者无法轻易生成,但存储在大约 20
作为一个学生项目,我们正在构建一个机器人,它应该跑完规定的路线并捡起一个木制立方体。它的核心是一台运行 debian 的单板计算机,配备 ARM9,频率为 250MHz。因此 Controller 的
在将 short 转换为字节数组时,我在网上找到了以下解决方案,但不太理解所涉及的逻辑。 //buffer is an array of bytes, bytes[] buffer[position]
如何在 PHP namespace 环境中检查对象的类而不指定完整的命名空间类。 例如,假设我有一个对象库/实体/契约(Contract)/名称。 以下代码不起作用,因为 get_class 返回完整
我有一个 View 范围的托管 bean,其托管属性绑定(bind)到查询字符串参数。 JSF 给了我熟悉的异常: javax.faces.FacesException: Property reset
根据 this post我已经修复了对象检查器。有时代码可以很好地运行 10 个条目,使它们全部正确,有时它可以运行 5 个条目。有时它会导致条目错误。 在获取元素的内部文本时总是会失败。当它的 Y/
我正在编写一组工具,其中 C++ 应用程序使用 AES 加密标准对数据进行编码,而 Java 应用程序对其进行解码。据我所知, key 长度必须为 16 个字节。但是当我尝试使用不同长度的密码时,我遇
我有以下代码: short num_short = 1; int possible_new_short = 1; valid = 1; while (valid) { poss
因此,作为 C 的新手,我遇到了我的第一个 SIGSEGV 错误。它出现在一个简短的 C 程序中,该程序旨在成为“猜数字”游戏。它由一个比较两个数字的自定义函数和一个带有输入的 do-while 循环
我不是严格意义上的初级程序员,但我没有接受过数学以外的正规教育 - 所以这纯粹是业余爱好,可能是业余的。 我最近自己开发了一个算法来解决这个问题,但我想知道是否有任何相对简单的算法明显更高效/更快?
我正在使用短条件来区分记录列表中显示的值。 例如,如果我希望强调 ( ) 标识符大于 100 的客户的姓名,请执行以下操作: {# Displays the identifier of the c
我是一名优秀的程序员,十分优秀!