简体   繁体   English

从 JavaScript 中的变量中减去 0 有什么好处?

[英]What is the benefit of subtracting 0 from a variable in JavaScript?

I saw a Javascript function that starts like this:我看到一个 Javascript function 开头是这样的:

var _0xadf3 = function(_0x29ae0e, _0x3626db) {
_0x29ae0e = _0x29ae0e - 0x0;
var _0x5edbe5 = _0x4430[_0x29ae0e];
if (_0xadf3['ogYkVt'] === undefined) {
    (function() {

What is the purpose of the first line of code (_0x29ae0e = _0x29ae0e - 0x0;)?第一行代码的目的是什么(_0x29ae0e = _0x29ae0e - 0x0;)? The first line of code takes the first variable of the function, _0x29ae0e, and subtracts 0 from it.第一行代码取function的第一个变量_0x29ae0e,减去0。 Thus, it seems that you will just be left with the original value.因此,您似乎只剩下原始值。 This seems unnecessary, and I would like to understand the reason.这似乎没有必要,我想了解原因。

Subtracting by zero is a way to force type-conversion.减零是一种强制类型转换的方法。 From Type-Conversion tutorial来自类型转换教程

Any mathematical operator except the concatenation/addition operator will force type-conversion.除连接/加法运算符外的任何数学运算符都将强制进行类型转换。 So conversion of a string to a number might entail performing a mathematical operation on the string representation of the number that would not affect the resulting number, such as subtracting zero or multiplying by one.因此,将字符串转换为数字可能需要对数字的字符串表示形式执行不会影响结果数字的数学运算,例如减零或乘以一。

Subtracting by 0x0 in this case performs a forced type-conversion on _0x29ae0e to convert it to hex在这种情况下减去0x0会对_0x29ae0e执行强制类型转换以将其转换为十六进制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM