JavaScript: implicit conversion, explicit conversion, implicit operation, explicit operation

1. Understand js implicit conversion

Implicit conversion in JavaScript refers to data type conversion that occurs automatically during execution without explicitly calling the conversion function. That is, JavaScript will automatically perform type conversion when operating with values ​​of different types. This conversion usually occurs when performing operations or comparisons between different data types.

serial number Classification
1 Conversion between strings and numbers
When using the plus (+) operator to concatenate strings and numbers, JavaScript will convert the numbers into string types to concatenate strings and numbers.
2 Convert a string to a number
When using the minus (-) operator, JavaScript converts the string to a number type for operation.
3 Conversion between Boolean values ​​and numbers
When using Boolean values ​​and numbers for operations, JavaScript will convert the Boolean value into a number type, true into 1, false into 0.
4 Conversion between objects and primitive values
​​When using objects and primitive values ​​for operations, JavaScript will automatically convert the object into a primitive value type. For example, when converting an object into a string, the object's toString() method will be called.
5 Implicit conversion to Boolean value
When a non-Boolean value is used in the judgment condition, JavaScript will convert it into a Boolean value, such as converting the number 0, empty string, null, undefined, NaN, etc. to false, and converting other values ​​to true. .
serial number type Example
1 Addition of strings and numbers (implicit conversion to strings) "1" + 2 // "12"
2 Addition of strings and booleans (implicit conversion to strings) "true" + true // "truetrue"
3 Addition of numbers and Boolean values ​​(implicit conversion to numbers) 1 + true // 2
4 Addition of arrays and strings (implicit conversion to strings) [1, 2, 3] + "hello" // "1,2,3hello"
5 Comparison of numbers and strings (implicit conversion to numbers) "1" > 2 // false
"1" < 2 // true
6 Multiplication of Boolean values ​​and numbers (implicit conversion to numbers)
Addition of boolean values ​​and numbers (implicit conversion to numbers)
true * 2 // 2
true + 1 // 2
7 Comparison of objects and Boolean values ​​(implicit conversion to Boolean values) if ({}) {   // The code here will be executed }

8 Numeric type as conditional conversion to boolean (implicit conversion to boolean) var num = 0;
if (num) {   console.log("num is true"); // Will not be executed until this point }

9 Conversion of undefined and null
When converting undefined to a numeric type, the result is NaN
When converting null to a numeric type, the result is 0
When converting undefined and null to a Boolean type, the result is false
Number(undefined) // NaN
Number(null) // 0
Boolean(undefined) // false
Boolean(null) // false
10 Add, subtract, multiply, and divide strings and numbers "10" + 1 // "101"
"10" - 1 // 9
"10" * 2 // 20
"10" / 2 // 5
"10" / "two" // NaN
11 When an object participates in the addition operation, the object's valueOf()method or toString()method is automatically called to convert the object into a string or number.

1. If the object has valueOf()a method, JavaScript will call that method first. valueOf()The method returns the original value of the object, usually a number or Boolean value.

2. If the object has no valueOf()method, or valueOf()the method returns a non-primitive value, JavaScript will try to call the object's toString()method. toString()The method returns a string representing the object.

3. If the object has no valueOf()method and toString()method, or both methods return a non-primitive value, JavaScript will throw a type error (TypeError).

let obj = {
  valueOf: function() { return 42; },
  toString: function() { return "42"; }
}

console.log(obj + 1); // 43

let obj2 = {
  toString: function() { return "2"; }
}

console.log(obj2 * 3); // 6

let obj3 = {}

console.log(obj3 + "hello"); // [object Object]hello

Implicit conversions in JavaScript can sometimes lead to unexpected, unintended results, especially when developers are unfamiliar with implicit conversion rules. Therefore, implicit conversions need to be fully understood and considered when writing JavaScript code. At the same time, in order to avoid unnecessary trouble caused by type conversion, for example, when using "==" or "!=" for comparison, implicit conversion will be performed. Strict equality ("===" or " "!==") operator for comparison. When writing code, try to avoid implicit conversions and perform type conversions explicitly.

2. Understand js explicit conversion

在 JavaScript 中,显式转换是指通过代码的明确操作将一个数据类型转换为另一个数据类型。

序号 方法 解释
1 parseInt() 将字符串转换为数字
parseInt() 函数用于将整数字符串转换为整数
parseFloat() 函数用于将浮点数字符串转换为浮点数
如果无法转换,则返回 NaN。
2 parseFloat()
3 Number() 将字符串、布尔值、undefined 和 null 转换为数字
4 String() 将数值、布尔值、对象、数组、null 和 undefined 转换为字符串
5 Boolean()函数 将任何类型的值转换为布尔值。其中,只有 null、undefined、false、空字符串和数字 0 被转换为 false,其他所有值都被转换为 true
6 操作符(+、-、*、/) 将数值字符串和布尔值转换为数字
7 Object()函数 将基本类型的数据转换为对应的对象类型。

显式转换在编程时非常常见,可以使程序更加明确和可读。但是要注意,如果显式转换不当,也可能会在程序运行时产生错误。因此,在进行显式转换时,要仔细考虑转换的类型和转换的方式,以避免潜在的错误。

三、理解js隐式操作

在 JavaScript 中,有很多隐式操作,这些操作是指在执行时自动发生,而不需要显示的操作(即不需要明确调用的操作)。

序号 方法 解释
1 隐式类型转换 在进行运算或比较时,JavaScript 会自动将不同类型的值转换成同一类型进行操作。例如,字符串和数字进行运算时,JavaScript 会将字符串转换成数字。
2 隐式原型链 当使用对象的属性时,如果该属性不在该对象的属性列表中,JavaScript 会沿着原型链向上查找该属性。这是一个自动发生的过程,称为隐式原型链。
3 隐式闭包 当一个函数内部引用了外部的变量时,JavaScript 会自动创建一个闭包。这个闭包会将被引用的变量保存在内存中,直到该闭包被销毁。
4 隐式对象创建 当使用对象字面量或 new 关键字创建对象时,JavaScript 会自动为该对象创建一个原型对象。
5 隐式函数调用 当使用函数调用时,如果函数的调用方式不是以对象方法的形式调用,那么调用的函数会成为全局对象的一个方法,此时 this 指向全局对象。
6 变量提升 在JavaScript中,变量和函数的声明会在代码执行前进行“提升”,即将声明提前执行,使得在声明之前就可以使用这些变量或函数。
7 this关键字 this关键字在不同的上下文中会有不同的值,例如在函数内部指向函数的执行上下文,而在对象方法中指向该对象。
8 隐式迭代 JS中的数组和对象都可以进行迭代操作,例如使用for-in和for-of循环来遍历对象和数组。
9 隐式全局变量 如果在函数中没有使用var或let关键字,则会隐式创建一个全局变量。
10 隐式递归 某些函数会在函数内部调用自身来实现递归操作,这种递归操作是隐式的。

四、理解js显式操作

在 JavaScript 中,显式操作是指通过代码的明确操作来实现某种操作或实现某种行为。

序号 方法 解释
1 赋值操作符(=) 将一个值赋给一个变量。
2 数学操作符(+、-、*、/、%) 用于数学计算,例如将两个数字相加或相乘等。
3 比较操作符(>、<、>=、<=、==、!=) 用于比较两个值的大小或相等性。
4 逻辑操作符(&&、||、!) 用于逻辑计算,例如判断两个条件是否同时成立、或者取反一个值等。
5 条件操作符(?:) 用于根据条件返回不同的值。
6 函数调用 使用函数名称和参数列表来调用一个函数。

通过显式操作,我们可以方便地控制程序运行的过程,实现我们想要的功能。同时,显式操作也常常被用来进行数据类型转换、类型检查等操作,进一步增强程序的健壮性和可维护性。

五、欢迎交流指正。

参考链接

JavaScript中的隐形转换

JS的隐式转换与显式转换_js隐式转换和显式转换_厉飞雨123的博客-CSDN博客

一文带你了解JavaScript的隐式类型转换_javascript隐式转换_ajaxsync的博客-CSDN博客

Guess you like

Origin blog.csdn.net/snowball_li/article/details/132691313