在javascript中表达式之前的+运算符:它做了什么? [英] + operator before expression in javascript: what does it do?

查看:164
本文介绍了在javascript中表达式之前的+运算符:它做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 underscore.js 库,我发现了一些东西以前没见过:

I was perusing the underscore.js library and I found something I haven't come across before:

if (obj.length === +obj.length) { ... }

那个 + 运营商在那里做什么?对于上下文,这里有一个直接链接到该部分文件。

What is that + operator doing there? For context, here is a direct link to that part of the file.

推荐答案

一元 + 运算符可用于转换JavaScript中的数字值。 Underscore似乎测试 .length 属性是一个数字,否则它将不等于自己转换为数字。

The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number.

这篇关于在javascript中表达式之前的+运算符:它做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆