首页 > 编程语言 >[Javascript] The hidden transform in Javascript

[Javascript] The hidden transform in Javascript

时间:2024-10-10 14:46:57浏览次数:8  
标签:primitive obj Javascript transform value return false hidden empty

[] + []

Answer: ""

Both arrays ([]) are first converted to their string representations before the + operator is applied. In JavaScript, arrays are converted to strings by concatenating their elements with commas in between. Since the arrays are empty, their string representations are empty as well.

 

[] + ![]

Answer: "false"

  • ![] is evaluated first:

    • In JavaScript, [] (an empty array) is truthy, so ![] evaluates to false.
  • Now the expression becomes [] + false:

    • The + operator, when used with an array and a non-array value, coerces both operands to strings.
    • [] (empty array) is coerced to an empty string "".
    • false is coerced to the string "false".
  • String concatenation:

    • "" + "false" results in "false".

 

Object to primitive

Let's go deep to understand why is so in low level

obj + obj = ?

When javascript see +operator, it will first convert Object to primitive type, it follow rules:

First, will check whether has [Symbol.toPrimitive]: fn ()

const obj = {
  [Symbol.toPrimitive]: function () {
     return 'wef' // here return value has to be a primitive type, if you return {},[], it will throw error
  }
}

console.log(obj + 1) // wef1

 

If we don't have [Symbol.toPrimitive], then it will check valueOf()

const obj = {
  valueOf() {
    return 'wef' // has to be primitive value
  }
}

console.log(obj + 1) // wef1

 

Lastly we check against toString()

const obj = {
  valueOf() {
    return {} // won't throw error, but also it won't be able to convert to primitive
  },
  toString() {
    return "wef"
  }
}

console.log(obj + 1) // wef1

 

If all of those do not exist, then it throw TypeError: Cannot convert object to primitive value

 

Now let's back to the question and try again

[][Symbol.toPrimitive] // undefined
[].valueOf() // [], not a primitive
[].toString() // ''
[1,2,3].toString() // '1,2,3'

[] + [] // '' + '' = ''

 

But ![] is different! It not doing object to primitive convertion, it's doing boolean check. And ![] is false

[] + ![] // '' + false = 'false'

For boolean check, those values are consdiered as falsey value

null, undefined, 0, NaN, false, ""

 

标签:primitive,obj,Javascript,transform,value,return,false,hidden,empty
From: https://www.cnblogs.com/Answer1215/p/18456341

相关文章

  • 深入探讨JavaScript中的精度问题:原理与解决方案
    深入探讨JavaScript中的精度问题:原理与解决方案在日常的JavaScript开发中,我们经常会遇到一些令人困惑的数值计算问题,特别是涉及到小数点运算时。例如,为什么0.1+0.2的结果不是预期的0.3,而是0.30000000000000004?本文将详细介绍JavaScript中出现精度问题的原因,深入解析十进......
  • JavaScript Number研究03_实例方法_toExponential_toFixed_toPrecision_toString_valu
    JavaScriptNumber研究03:实例方法——toExponential、toFixed、toPrecision、toString、valueOf、toLocaleString在JavaScript中,Number对象不仅包含了许多有用的静态属性,还提供了一系列实例方法,帮助我们在不同场景下处理和转换数值。这些方法包括:toExponential()toFixed()......
  • CSS:transform-origin
    transform-origin:设置旋转元素的基点位置,即旋转轴的位置。而transform主要是平移和旋转的功能。使用transform-origin属性必须先使用transform属性(旋转)transform-origin:x-axisy-axisz-axis;默认值为:50%50%0x-axis的值可为left、center、right、lenght、%,left,center......
  • 【AI大模型】深入Transformer架构:编码器部分的实现与解析(下)
      目录......
  • 【JavaScript实用日期星期函数】日期格式化、获取日期是星期几、今后7天的日期、本周
    ......
  • transformers和bert实现微博情感分类模型提升
    关于深度实战社区我们是一个深度学习领域的独立工作室。团队成员有:中科大硕士、纽约大学硕士、浙江大学硕士、华东理工博士等,曾在腾讯、百度、德勤等担任算法工程师/产品经理。全网20多万+粉丝,拥有2篇国家级人工智能发明专利。社区特色:深度实战算法创新获取全部完整项目......
  • 深度学习No module named ‘torchvision.transforms.functional_tensor‘问题解决
    问题在进行深度学习训练过程中出现ModuleNotFoundError:Nomodulenamed'torchvision.transforms.functional_tensor'报错,多方查阅资料后得到了解决方案。关于我的环境:CUDA==12.1torch==2.4.1GPU==4090D原先进行深度学习用的CUDA11.3,torch1.2.1,但是在训练时出现nvrtc......
  • 如何使用Transformer模型处理金融文本数据?
    《DeepLearningforFinance》这本书主要介绍了如何使用深度学习技术,特别是机器学习和强化学习方法来处理金融数据,但并没有详细讨论Transformer模型在金融文本数据处理中的应用。不过,基于深度学习的一般知识以及Transformer模型的广泛用途,我们可以构建一个关于如何利用Tran......
  • JavaScript中的运算符
    一、运算符分类运算符:(operator):也称之为叫操作符。是用来实现赋值、比较和执行运算等功能的符号javaScript中常用的运算符:算数运算符递增和递减运算符比较运算符逻辑运算符赋值运算符1.算数运算符概述:算数运算使用的符号,用于执行两个变量或值得算数运算运算符描述......
  • [Javascript] Using defineProperty to observe the object props changes
    constobj={a:1,b:2,c:{a:1,b:2,},};functionisObject(val){returnval!==null&&typeofval==="object";}functionobserve(obj){for(letkeyinobj){letv=obj[key];if(isObject(v)){......