JavaScript fourth edition reading ing~(1)

Next, I will read the fourth edition of JavaScript. I am very busy with my work. I will try to do a daily update to consolidate the foundation. From the third chapter onwards, I will write reading notes.

third chapter

3.3 Variable var let const
1.
This line of code of var message defines a variable named message, which can be used to save any value type. When it is not initialized, it will save a special value undefined, or it can be initialized and assigned directly to'hi ', but it does not mean that the message is a string type, it is just a simple assignment.

2. The so-called variable promotion, and there is no problem in repeatedly claiming a var variable.

function foo(){
    
    
			console.log(age)
			var age =26
		}
		foo(); //undefined
		等价于:
		function foo(){
    
    
			var age;
			console.log(age);
			age = 26
		}
		foo(); //undefined

3. The temporary dead zone of
let An important difference between let and var is that the variable of let's reputation will not be promoted in the scope.

//name会被提升
console.log(name);//undefined
var name = 'Matt'//age不会被提升
console.log(age);//ReferenceError:age未定义
let age= 17

Of course, the book here says that the Javascript engine will also pay attention to the let's reputation that appears later, but before that, you can't refer to the unnamed variable in any way.
Different from the var keyword, let's global reputation variables will not become the properties of the window object.

var name = 'Matt'
console.log(window.name);//Matt
let age= 17;
console.log(window.age);//undefined

let will be restricted to the scope of the block, where name is equivalent to assignment in the global
if(typeof name =='undefined'){ let name; } name ='zz';


4. const
const and let are basically the same. The only difference is that the variable with its reputation must be initialized, and attempts to modify the variable with the const reputation will cause runtime errors. And it is not allowed to repeat the reputation.
The const reputation only applies to the reference of the variable it points to. Examples are as follows:

const person = {
    
    }
person.name = 'zz'//ok的

3.4 Data Types
ECMAScript has 6 data types: Undefined, Null, Boolean, String, Number, Symbol
typeof will return object when detecting null, because the special value null will be considered as a null object reference.

typeof null //object

3.4.4 Boolean() to value
Insert picture description here
3.4.5 Number

NaN == NaN //false

isNaN用来判断这个参数是“不是数值”,它尝试转,任何不能转为数值的值都会导致函数返回true.
isNaN内部调用了对象的valueOf()方法,以及toString().

isNaN(false)//false,可以转为0

Number() conversion rules:

  1. The value is returned directly
  2. null returns as 0
  3. undefined is returned as NaN
  4. String: Zero in front will be ignored, empty string is 0, hexadecimal is converted to decimal, etc.
let num1 = Number('Hello world')// NaN
let num2 = Number('') // 0
let num3 = Number('000011') //11
let num4 = Number(true)  //1

parseInt() conversion rules

  1. The preceding space will be ignored, and the conversion will start from the first non-blank character. If it is not a numeric character, plus or minus sign, NaN will be returned immediately, which means that a non-empty string will also return NaN, which is different from Number() , Continue to detect until the end or a non-numeric character is reached
let num1 = parseInt('1234blue') //1234
let num2 = parseInt('')  //NaN
let num3 = parseInt('22.5') //22

parseInt支持第二个参数,代表多少进制,当然写了第二个参数,第一个参数就可以省略进制
let num4 = parseInt('0xAF',16) //175
let num5 = parseInt('AF', 16) //175

parseFloat() conversion rules

  1. Similar to parseInt, parse to the end of the string or the end of the decimal point, which means that the second decimal point is invalid, and hexadecimal always ignores the 0 at the beginning of the string, because this function directly parses the decimal value (here I try After a while, let num1 = parseFloat (0xAF) is normally converted to 175, but this should be pure console.log (0xAF) is also 175, so just understand it, parseInt() and parseFloat() are more focused on characters string)
let num1 = parseFloat('1234blue')// 1234
let num2 = parseFloat('0XA') // 0
let num3 = parseFloat('22.5') // 22.5
let num4 = parseFloat('22.23.5') // 22.23
let num5 = parseFloat('0908.5') // 908.5
let num6 = parseFloat('222') // 222

ok here today, off work~~

Guess you like

Origin blog.csdn.net/weixin_46013619/article/details/109983985