Fundamental Syntax
To name a let variable only by number,$,letter,_,and can’t begin with number likelet 4myName = 'Jerry Yu'
Value is either an object or primitive value in JS:
//Objec |
7 primitive data types:
⚠️It’s the value that has a type,not the variable!!!
console.log(typeof null)
会输出Object(本应为null)这是js的历史遗留问题,并且这个bug无法被修复,因为这涉及到需要修改底层的ASCⅡ字符编码。
template literal (strings):
//template strings: |
type conversion(explicitly manually convert by programmer) and type coercion(implicitly automatically convert by JS)
type coercion:
console.log('23' + '1' - 1); |
result:
230(number) |
当JS做 type coercion时,string和number做减法时string被转成number参与运算,而string和number做加法时number被转成string参与运算。
不论什么情况有string参与/ *运算string都转成number参与运算。
empty object {}
will be seen as FALSE
when it is converted to Boolean type.
==
(宽松相等 / Loose Equality)
- 特点: 比较时,如果两个值的类型不同,JavaScript 会尝试进行类型转换 (Type Coercion),将它们转换成相同的类型,然后再进行值的比较。
console.log(1 == "1"); // true (字符串 "1" 被转换为数字 1) |
===
(严格相等 / Strict Equality)
- 特点: 比较时,不进行任何类型转换。只有当两个值的类型相同且值也相同时,才返回
true
。
NaN === NaN
为 false
(这是 JS 中 NaN
的特殊行为,NaN
不等于自身)。
console.log(1 === "1"); // false (类型不同,一个是 number,一个是 string) |
statement and expression
expression: small pieces of code、be able to produce value.
statement: larger pieces of code、may contain several expression and a series of actions that we want the program to perform.
In the first line of .js file codes'use strict';
will activate the strict mode to write more secure code.
Anonymous function:
; |
We can call Function Declaration before define it but can’t do the same to Function Expression because of a process called hoisting.
; |
// Arrow function: |
Note: Arrow function can’t use this
keyword.
const yearsTillDie = (birthYear, myname) => { |
Array in JS can contain different type of values:
const friends = ['Daria', 'Todd']; |
Basic methods of array:
const friends = ['Daria', 'Todd']; |
Object:
const jerry = { |
在vscode中windows ctrl+d 可以用multiple cursor一次操作多个相同值,非常方便。(前题是在vscode中设置了)
let dice = Math.trunc(Math.random() * 6) + 1; |
在vscode中control+b可以展开或关闭侧边栏。
DOM
const score0 = document.getElementById("score--0"); |
Deeper understanding to JS
arrow function don’t own arguments object and this
keyword in its execution context .But, they can use the arguments object and this
key word from their closest regular function parent.
let
,const
and function are blocked scoped.var
is function scoped.
用var
创建的变量会成为global window Object的一部分:
var x = 1; |
output: |
arrow function的this
key word 是它的 parent function 的this
key word.
定义object的{}并不创建scope:
const jerry = { |
输出I am undefined
这里this的parent scope是global scope。this
在global中应该为window Object 而 window Object 无age属性。
VSCODE Short Cut Key
在vscode中control+b可以展开或关闭侧边栏。
alt+箭头 可以上下移动代码块。
在vscode中windows ctrl+d 可以用multiple cursor一次操作多个相同值,非常方便。(前题是在vscode中设置了)