Basic
Primitive types
- Boolean (true, false)
- Number (12, 1.618, -46.7, 0, etc.)
- String (“hello”, “world!”, “12”, etc.)
- Null (no value)
- Undefined (declared but not yet assigned a value)
- Symbol
- BigInt
Const,let and var
| hoist | scope |
---|
var | Only declarations are hoisted | function scope | let const | block hoisted,but not initialized with ‘undefined’ | block scope |
TDZ: from the start of the block until the initialization has completed.
block:{}
x = 1;
console.log(x + " " + y);
var y = 2;
console.log(num);
let num="fasd";
function varTest() {
var x = 1;
{
var x = 2;
console.log(x);
}
console.log(x);
}
function letTest() {
let x = 1;
{
let x = 2;
console.log(x);
}
console.log(x);
}
function go(n) {
console.log(n);
for (let n of n.a) {
console.log(n);
}
}
go({a: [1, 2, 3]});
Symbol
-
Symbol.for(key) Searches for existing Symbols with the given key and returns it if found. Otherwise a new Symbol gets created in the global Symbol registry with key. -
Symbol.keyFor(sym) Retrieves a shared Symbol key from the global Symbol registry for the given Symbol.
Operators
-
“==” forces the arguments to be of the same type before comparing them use a===b -
'+'触发隐式类型转换
Function
- Arrow Function:
- Does not have its own bindings to this or super, and should not be used as methods.
- Can not be used as constructors.(new)
- Traditional Function:
(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number
Ouput
console.log();
alert("");
Object
Declaration
let person={
name:"ycy",
age:29,
color:"red"
};
let {name,age}=person;
Array
let a=[1,2,3];
a.splice(1,1,4);
a.concat(1,[2,3],[5,[6,7]])
a.map(x=>x+3);
a.foreach(value=>sum+=value);
a.filter(x=>x>2);
a.reduce((x,y)=>x+y);
Other function
Category | Function | Notes |
---|
find | indexOf(),lastIndexOf() | starts at the beginning/end | fill | fill(value: number, start?: number, end?: number) | start default:0,end default:array.length | stacks & queues | push(),pop() | shift(),unshift() | delete index 0,insert index 0 |
Closure
this is determined by the enclosing lexical context
Global Context
function f1() {
return this;
}
f1() === window;
f1() === globalThis;
Class Context
Within a class constructor, “this” is a regular object.
All non-static methods within the class are added to the prototype of “this”
Inside Object
Object有自己的Execution context,但没有this.
When a function is called as a method of an object, “this” is set to the object the method is called on.
var obj = {
bar: function() {
var x = (() => this);
return x;
},
doSomethingLater : function (){
setTimeout(function(){
this.count++;
console.log(this);
}, 300);
}
};
var fn = obj.bar();
console.log(fn() === obj);
var fn2 = obj.bar;
console.log(fn2()() == window);
obj.doSomethingLater();
DOM
Document Object Model: how your browser interprets the HTML “tree”
<ul id="shopping list">
<li>milk</li>
</ul>
-
Create and add elements on the fly const list=document.getElementById("shopping list");
const newEL=document.createElement("li");
newEl.innerHTML="apples";
list.appendChild(newEl));
-
Change the contents/style of an element const list=document.getElementById("shopping list");
list.style.color="red";
-
Give elements actions by adding event listeners const button=document.getElementById("button");
button.addEventListener("click",()=>{
addParagraph("");
});
Promise
实现promise.all
function all(promises) {
return new Promise((resolve,reject)=>{
if(!Array.isArray(promises))reject(new TypeError("参数错误"));
let results=[];
if(promises.length==0)resolve(promises);
promises.forEach(promise=>{
Promise.resolve(promise).then(value=>{
results.push(value);
if(results.length===promises.length)
resolve(results);
},err=>reject(err));
})
}
)
}
|