当前位置: 首页> 文旅> 艺术 > 是怎么开的?_yellow片高清影视在线观看_16888精品货源入口_建站模板网站

是怎么开的?_yellow片高清影视在线观看_16888精品货源入口_建站模板网站

时间:2025/8/1 19:18:50来源:https://blog.csdn.net/qq_68155756/article/details/146972188 浏览次数:1次
是怎么开的?_yellow片高清影视在线观看_16888精品货源入口_建站模板网站

在 JavaScript 中,一元减号运算符(-)用于对操作数的值取反。

1. 数字类型 - 直接取反:
console.log(-42);   // -42
console.log(-3.14); // -3.14

 

2.字符串类型 - 若字符串是有效数字,转换为数字后取反:

console.log(-"123");  // -123
console.log(-"12.3"); // -12.3

 

3.布尔类型 - true 转为 1false 转为 0

console.log(-true);  // -1
console.log(-false); // -0

4.对象类型 - 调用 valueOf() → toString() 转换为原始值,再取反:
const obj = { valueOf: () => 42 };
console.log(-obj); // -42const obj2 = { toString: () => "100" };
console.log(-obj2); // -100const obj3 = {}; // 默认转换为 "[object Object]" → NaN
console.log(-obj3); // NaN

 

5.null 和 undefined - null 转为 0undefined 转为 NaN
console.log(-null);     // -0
console.log(-undefined); // NaN

6.BigInt 类型 - 支持直接取反:
console.log(-5n); // -5n

 

注意事项

  • NaN 结果:若操作数无法转换为有效数字,结果为 NaN
  • 对象转换:依赖对象的 valueOf 和 toString 方法,需确保逻辑可控。

关键字:是怎么开的?_yellow片高清影视在线观看_16888精品货源入口_建站模板网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: