In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "javascript unpacking and type conversion", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "javascript unpacking and type conversion method" article.
Basic data types: string, number, boolean
Reference types: object, function
Type that does not exist: undefined
String, Number and Boolean belong to the wrapper types of string, number and boolean, respectively, and their objects belong to the reference type.
Boxing
Boxing refers to the operation of converting basic data types into corresponding reference types, which mainly refers to the data of string, number and boolean types, which are packaged into reference type data by String, Number and Boolean.
/ / implicitly boxed var S1 = 'Hello World'; var S2 = s1.substring (2)
The execution steps of the second line of code above actually look like this:
Create a temporary instance object using new String ('Hello World')
Call the substring method with a temporary object
Assign the execution result to S2; destroy the temporary instance object.
The above steps are translated into code, as follows:
/ / explicitly packing var S1 = 'Hello World'; var tempObj = new String (' Hello World'); var S2 = tempObj.substring (2); unpacking
Unpacking is to convert a reference type to a basic data type.
About ToPrimitive in the process of unpacking
Type conversion
Operators have an expectation type for variables at both ends, and in javascript, implicit conversions are made for variables that do not satisfy the operator's waiting type.
Logical operator
There is only one standard for implicit conversion when performing logical operations: only null, undefined,'', NaN, 0, and false represent false, and other cases are true, such as {}, [].
Arithmetic operator
If both ends of the arithmetic operator are of type number data, evaluate directly
If there is a non-number basic data type at both ends of the arithmetic operator, the non-number Operand is boxed with Number (), and then the return value is unboxed to number type to participate in the calculation.
If there is a reference data type at both ends of the arithmetic operator, the reference type is unboxed first, and if the result is a non-number type, it is executed according to condition 2, otherwise condition 1 is executed.
1-true / / 0, first convert Number (true) to the number 1, then execute 1-11-null / / 1, first convert Number (null) to the number 0, then perform 1-01 * undefined / / NaN, Number (undefined) into the number NaN, and then execute 1 * NaN2 * [5'] / 10 ['5'] unpacking according to the ToPrimitive rule will become'5', then unpacking through Number ('5') will be changed to the number 5123 + {valueOf: () = > {return 10}} / / 133{ valueOf: () = > {return 10}} according to the ToPrimitive rule, valueOf will be called first, and the result is 10.
When + appears before a variable as a unary operator, it means to convert the variable to a Number type
+ "10" / / 10 is the same as Number ("10") + ['5'] / / 5 ['5'] according to ToPrimitive rules, it becomes'5', and then it becomes a numeric 5-string concatenation through the unpacking operation of `Number`.
The symbol of a string connector is the same as the + of the arithmetic operator.
If both ends of the arithmetic operator are data of type string, join directly
If there is a non-string primitive type at both ends of the operator, the non-string primitive type data is boxed with String (), and then the return value is unboxed to participate in string concatenation.
When there is a reference data type at both ends of +, the reference type is unboxed first. if the result is a non-string type, it is executed according to condition 2, otherwise condition 1 is executed.
Relational operator
NaN and any other type, doing any relational operation always returns false (including himself). If you want to determine whether a variable is NaN, you can determine it through Number.isNaN ().
Null = = undefined the comparison result is true, except that the comparison value of null, undefined, and any other result (not including themselves) is false.
Here is the rule definition, null is the type of object, but there will be syntax errors when calling valueOf or toString. Just remember the result here.
General situation:
If both ends of the arithmetic operator are of type number data, evaluate directly
If there is a non-number basic data type at both ends of the arithmetic operator, the non-number Operand is boxed with Number (), and then the return value is unboxed to number type to participate in the calculation.
If there is a reference data type at both ends of the arithmetic operator, the reference type is unboxed first, and if the result is a non-number type, it is executed according to condition 2, otherwise condition 1 is executed.
{} =! {} / / false Number ({} .valueof (). ToString ()) = > NaN, so the topic is equivalent to NaN = = false, NaN and any type of comparison is false [] = = [] / / false memory address is different! [] = = 0 / / true! [] = > false, so the topic is equivalent to false==0, Number (false) = > 0, so the result is true some questions
[] =! []
-the first step,! [] will become false-the second step, the valueOf of [] is [], [] is the reference type, continue to call toString, and the title becomes: "= = false-third step, convert both ends of the symbol to Number, and get zero zero-so, the answer is true.
[undefined] = = false
-in the first step, the valueOf result of [undefined] is [undefined], and then [undefined] becomes''through toString, so the topic becomes''= = false-step 2, the two ends of the symbol are converted to Number, and you get zero zero.-so, the answer is true!
How to make the result of axiomatic 1 & & axiomatic 2 & & axiomatic 3 be true
Var a = {value: 0, valueOf: function () {this.value + = 1; return this.value}}; console.log (a = = 1 & a = = 2 & a = 3) / / true
How to make the result of a===1&&a===2&&a===3 true
/ / use defineProperty for data hijacking var value = 0bot Object.defineProperty (window, "a", {get () {return + + value;}}) console.log (a===1&&a===2&&a===3) / / true
Implement an infinite accumulation function
Corialization to realize multi-parameter accumulation
The above is about the content of this article on "javascript unpacking and type conversion". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.