当前位置:网站首页>How to perform implicit conversion in JS
How to perform implicit conversion in JS
2022-07-27 05:41:00 【weixin_ forty-six million fifty-one thousand two hundred and si】
ToPrimitive Method : This is a JS The method implicit in each value in the , Used to set the value ( Whether it's a basic type value or an object ) Convert to basic type value
If the value is a basic type , Then directly return the value itself ; If the value is an object , It probably looks like this :
ToPrimitive(obj,type)
obj–> Objects that need to be transformed
type–> Expected result type
type The value of can be number perhaps string, The default is number
1) When type by number The rules of time are as follows :
- call obj Of valueof Method , If it's the original value , Then return to , Otherwise, the next step
- call obj Of toString Method , If it's the original value , Then return to , Otherwise, the next step
- Throw out typeError abnormal
2) When type by string The rules of tense are as follows :
- call obj Of toString Method , If it's the original value , Then return to , Otherwise, the next step
- call obj Of valueOf Method , If it's the original value , Then return to , Otherwise, the next step
- Throw out typeError abnormal
var objToNumber=function(value){
return Number(value.valueOf().toString())
}
console.log(objToNumber('')===0);//true
console.log(objToNumber({
})===NaN);//false
- + There is only one on either side of the operator string When type variable , Variables on both sides will be implicitly converted to strings ; In other cases, both variables will be converted to numbers
console.log(1+'23');//123
console.log(1+false);//1
console.log('1'+false);//1false
console.log(true+false);//1
- -、*、/ The operands are converted to numbers
console.log(20-'5');//15
console.log(1*false);//0
console.log(1/'aa');//NaN
- ==, Cast data types , Equivalent unequal type
- about < and > Comparison symbol , Compare in alphabetical order
console.log('a'>'b');//false
console.log('aa'<'bc');//true
- In other cases , Convert to numbers and compare
console.log(12<'13');//true
console.log(false<-1);//false
- object
var a={
}
console.log(a>2);//false
边栏推荐
猜你喜欢

Share a multiple-choice question about variables (including global variables, local variables, the scope of variables, and life cycle knowledge points)

Pytorch installation new pit

一本通1329 细胞(广度优先搜索)

Package JWT

使用vertical-align不能让图片和文字垂直居中对齐

「PHP基础知识」定界符的使用

权限展示-左侧列表动态化

攻防世界-mfw

Apache SSI 远程命令执行漏洞

分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
随机推荐
XSS knowledge points
Package JWT
退出登录与jsx显示
Program environment and preprocessing (Part 2): define, undef, command line compilation, conditional compilation, file inclusion (super full collation, recommended collection!!!
js基础练习题
分享一道关于#define的选择题(内含#define在预编译时的替换规则,程序环境和预处理相关知识)
初识C语言——字符串+转义字符+注释
信息学奥赛一本通1176题——谁考了第k名
First knowledge of C language - why does every C program have a main function
自我理解思考
JS==操作符的强制类型转换规定
map结构
[CISCN2019 华东南赛区]Web11 1
My first blog
Introduction and management of heap memory in C language
Promise的理解,以及它的实例方法
arguments
编辑删除用户
程序环境和预处理(上):一个程序是怎么成功运行的?
布局的搭建及天气预报的显示