js data type and an implicit type conversion

Stack memory

  • js There are two major memory: heap memory (heap), stack memory (stack)
  • Heap memory effect: used to store the content (key-value pairs are stored in the object, the function is stored in the code string)
  • Stack memory effect: can also be referred to as scopes, the code parsing and execution environment
Stack memory release issue

Heap memory:

Release heap memory as long as there are no variables occupy the heap memory, the browser will be free time to release it out, so we try to project the heap memory not being used manually released.

Stack memory:

Function performs will form a private account for memory, general function executed, the stack memory on their own release, unless there is a certain thing stack memory (for example: stack memory opened up the heap memory) is the stack memory unexpected variables (or other thing) takes up at this time of stack memory can not be freed; global scope (stack memory) is loaded js at the beginning of the birth of the current page will be automatically closed when released.

Basic data types

Js the underlying data type, these values ​​have a fixed size, are often stored in the memory space, storage space is allocated automatically by the system. We can directly manipulate the value stored in the stack memory space, so the basic data types are accessed by value

基础数据类型:Number String Undefined Boolean

Reference data types

Since the value of reference data types stored too complex (complex structure that is more content), the rendering engine will open up a new memory space to store these values ​​alone, the last memory references all operations late address assigned to the corresponding variable, They are based on the address to find the space and then the contents of space to operate.

引用数据类型:Object Array Date RegExp Math Function
Object data types
  1. Open up a memory space (there is a hex address)
  2. The first attribute names and values ​​of objects stored in the memory space
  3. The address of the memory space assigned to the variable
Function Data Types
  1. Open up a memory space (there is a hex address)
  2. The body of the function code as a "string" is stored in the memory space
    • When function to create a string of meaningless so that not only creates the function execution is meaningless
    • Variable lift only under the current scope of the var/functionprocess, mainly due to the existence of a function are strings, the function we see in var/functionthis case also are strings it
    • When the function is executed, it is to string out this pile of execution
  3. The memory address space assigned to the variable

Often you encounter such a similar topic in the front-end interview

var a = 10;
var b = a;
b = 20;
// 这时a的值是多少?

When the data in the stack memory replication behavior occurs, the system automatically assigns a new variable to the new value. var b = aAfter execution, the value of a and b are equal to 10, but in fact they are already independent of the value of the affect each other.

var a = {name:'小明',age:'10' };
var b = a;
b.name = '小红'
// 这时a.name的值是多少?

We var b = acopy the reference type of operation is performed once. Reference type of copy will also automatically assigns a new value is stored in the stack memory for the new variable, but the difference is that this new value, just a reference type an address pointer. When the address pointer is the same, although they are independent, but the access to specific objects in the heap memory is actually the same.

Implicit type conversion

Js data type is very weak, when using the arithmetic operators, arithmetic operators on both sides may be any type of data, for example, a string and a number can be added. The reason why the operation can be done between different data types, because js engine before the operation will quietly put them implicit type conversion.

Let's look at a typical unit operators to manipulate the results in js

console.log([] == []) // false
console.log([] == ![]) // true
console.log([] !== [])  // true
console.log(NaN != NaN) // true
console.log(null == undefined) // true
console.log(null === undefined) // false
console.log(1 == true) // true
console.log(null > 0) // false
console.log(true + 1) // 2
console.log(undefined + 1) // NaN
console.log({} + 1) // [object Object]1
console.log([] + {}) // [object Object]
console.log([2,3] + [1,2])  // 2,31,2
Original value Converted value type Into a string Into Boolean
false 0 "false" false
true 1 "true" true
0 0 "0" false
1 1 "1" true
"0" 0 "0" true
NaN NaN "NaN" false
Infinity Infinity "Infinity" true
"" 0 "" false
[ ] 0 "" true
[20] 20 "20" true
function(){} NaN "function(){}" true
{} NaN "[object Object]" true
null 0 "null" false
undefined NaN "undefined" false

Comparison operation

js provides us with a strict comparison with casts comparison between two models, a strict comparison (===) only consistent operating on both sides of the object operator type, and the content will be consistent returns true, otherwise it returns false. What is more widely used ==operator will first be converted to the same operation target type, making comparisons.

Comparing the operating values ​​equal operator will implicit conversion:

  • If the operation is a Boolean value, before comparing the first value is converted to its
  • If the operation is a string, the numerical value of another operation, through Number () function to convert a string value
  • If a target value of the operation, the other is not, then call the object's valueOf () method, the results obtained are compared according to the foregoing rules
  • null and undefined are equal
  • If the operation is a NaN, then returns false equality comparison
  • If the two values ​​are the operation target, the comparator are not the same object

Adding operator:
addition operator in splicing js also as a character string, so the rules of the addition operator is divided into two

  • If a number is NaN, then the result is NaN
  • If Infinity + Infinity, the result is Infinity
  • If -Infinity + (- Infinity), the result is -Infinity
  • If Infinity + (- Infinity), the result is NaN
    if there is an operation value is a string, then:
  • If the two are strings, stitching together
  • An operation value is a string, then further converted into a string value, then spliced ​​together
  • And if there is a target value, or a Boolean value, the call toString () method of obtaining a string value, and then apply the rules in front of the string. For undefined and null, respectively call String () explicitly converted to a string.

Logical operators (!, &&, ||)
logic NOT (!) By the first Boolean operator () function to convert its operation Boolean value, and then negated.

Logic (&&) operator, if the operation value is not a Boolean value, the following conversion rules:

  • If the first operand by the Boolean () after conversion to true, the second operating value is returned, otherwise the first value (not Boolean () value after the conversion)
  • If the operation has a value of null, null is returned
  • If there is an operation value NaN, NaN is returned
  • If there is an operation value is undefined, returns undefined

Logical OR (||) operator, if the operation value is not a Boolean value, the following rules:

  • (Not Boolean value () conversion) if the first value by a Boolean operator () after conversion to false, the second operating value is returned, otherwise, the operation returns a first value
  • And logic for processing rule undefined, null, and the same as NaN (&&)

The relational operators (<,>, <=,> =)
with the above-described operator, the operating values relational operators may also be of any type, the use of non-numeric type involved in comparison need system implicit type conversions:

  • If the two values ​​are the operating value, the numerical comparison
  • If the two values ​​are the operating string, character string encoded value corresponding to the comparator
  • If only one operation is a numeric value, then the value is converted to another operation values, numerical comparison
  • If the operand is a target, it is called valueOf () method (if the object is not valueOf () method is called toString () method), the result of comparison performed in accordance with the preceding rules
  • If a value is a Boolean operation, it is converted to a value, and then compared

Guess you like

Origin www.cnblogs.com/mengxiangji/p/11080006.html