IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> Javascript基础复习 -> 正文阅读

[JavaScript知识库]Javascript基础复习

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

hoistscope
varOnly declarations are hoistedfunction scope
let constblock hoisted,but not initialized with ‘undefined’block scope

TDZ: from the start of the block until the initialization has completed.

block:{}

/**Example 1:hoist ***/
x = 1; 
console.log(x + " " + y); // '1 undefined'
var y = 2; // Declare and Initialize y
console.log(num);//Cannot access 'num' before initialization
let num="fasd";
/**Example 2:scope ***/
function varTest() {
  var x = 1;
  {
    var x = 2;  // same variable!
    console.log(x);  // 2
  }
  console.log(x);  // 2
}

function letTest() {
  let x = 1;
  {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}
/**Example3: TDZ**/
function go(n) {
  // n here is defined!
  console.log(n); // Object {a: [1,2,3]}

  for (let n of n.a) { // ReferenceError,n.a先找block内的再找function内的
    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

  1. “==” forces the arguments to be of the same type before comparing them

    use a===b

  2. '+'触发隐式类型转换

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:
    • 每次调用都会创建this和arguments
//一个参数时()可省略
(param1, param2,, paramN) => { statements }
(param1, param2,, paramN) => expression
//相当于:(param1, param2, …, paramN) =>{ return expression; }
//常用函数
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number//timeout default:0

Ouput

console.log();
alert("");//generate a pop-up notification with the given content

Object

Declaration

let person={
    name:"ycy",
    age:29,
    color:"red"
};
let {name,age}=person;//Object destructuring is a shorthand to obtain multiple properties at once.

Array

let a=[1,2,3];
//改变数组
a.splice(1,1,4);//于p1删除p2个元素并在p1插入=>[1,4,3]
//不改变数组
a.concat(1,[2,3],[5,[6,7]])//[1,4,3,1,2,3,5,[6,7]]//flatten array,but not arrays of arrays
a.map(x=>x+3);//return [4,7,6] but not change
a.foreach(value=>sum+=value);
a.filter(x=>x>2);//return [4,3]
a.reduce((x,y)=>x+y);//8,reduce right process from right-to-left

Other function

CategoryFunctionNotes
findindexOf(),lastIndexOf()starts at the beginning/end
fillfill(value: number, start?: number, end?: number)start default:0,end default:array.length
stacks & queuespush(),pop()
shift(),unshift()delete index 0,insert index 0

Closure

this is determined by the enclosing lexical context

Global Context

function f1() {
  return this;
}
// In a browser:
f1() === window; // true
// In Node:
f1() === globalThis; // true

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); // true
// But caution if you reference the method of obj without calling it
var fn2 = obj.bar;
// Calling the arrow function's this from inside the bar method()
// will now return window, because 把函数拉到下面去了
console.log(fn2()() == window); // true
obj.doSomethingLater(); 
//In a browser:window,In Node:Timeout {...}

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("");
    });//common events include load, click, input, mouseover
    

Promise

实现promise.all

/**
 * @param {Array<any>} promises - notice input might have non-Promises
 * @return {Promise<any[]>}
 */
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));
    })
  }
  )
}
  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-05 10:43:24  更:2021-09-05 10:45:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 15:27:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码