React学习:ES6

参考视频

1. let,var, const 区别

js 复制代码
var x = 1;      // 函数作用域,可重复声明
let y = 2;      // 块级作用域,不可重复声明
const z = 3;    // 块级作用域,不可重新赋值

2. Objects

js 复制代码
const person ={
    name: 'Maximilian',
    age: 30,
    walk(){
        console.log("walk")
    }
}
// 访问对象属性的两种方式:
//1. 通过[]
console.log(person["name"])
//2. 通过 .
console.log(person.age)

3. this

如果使用对象的形式调用方法,this指向这个对象;但如果单独调用该函数,this并不指向该对象,具体而言:严格模式下:this 为undefined;非严格模式下:this 这时指向的是浏览器的全局对象,即window。

js 复制代码
const person ={
    name: 'Maximilian',
    age: 30,
    walk(){
        console.log(this)
    }
}


person.walk() //{name: 'Maximilian', age: 30, walk: ƒ}
const walk= person.walk
walk() // undefined because this is not bound to the object anymore

注意 :在js中,由于函数也是对象,因此单独调用该孤立函数时,可以通过.bind()方法绑定this指向的对象

js 复制代码
const walk= person.walk.bind(person)
walk() //{name: 'Maximilian', age: 30, walk: ƒ}

4 . Arrow Function

js 复制代码
const person ={
    name: 'Maximilian',
    age: 30,
    walk(){
        console.log(this)
    },
    talk(){
        //由于以孤立函数调用setTimeout,因此this默认是window对象
        setTimeout(function(){
            console.log(this)
        }, 1000)

        //使用箭头函数,不重新绑定this
        setTimeout(()=>{
            console.log(this)
        },1000)
    }
}

person.talk()

5. Array map && filter

js 复制代码
const colors=["red","blue","green"]

const items =colors.map(color=>`<li>${color}</li>`)
const list=colors.filter(color=> color === "red")

6. Object Destructing 解构

js 复制代码
const address={
    street:"1234",
    city:"New York",
    state:"NY",
    zip:"10001"
}

// const street=address.street
// const city=address.city
// const state=address.state
// const zip=address.zip

const{street,city,state,zip}=address

7. Spread Operator

把一个可迭代对象(数组、字符串等)或对象"展开"成独立的元素。

js 复制代码
const address={
    street:"1234",
    city:"New York",
    state:"NY",
    zip:"10001"
}

const person ={
    name:"John",
    age:30,
    address:address
}

// concat
const information={...address,... person}
// clone
const clone ={...address}

8. Named and Default

js 复制代码
// Default -> import ... from '';
// Named -> import {...} from ''
相关推荐
Ruihong16 小时前
Vue withDefaults 转 React:VuReact 怎么处理?
vue.js·react.js·面试
用户298698530141 天前
在 React 中使用 JavaScript 将 Excel 转换为 SVG
前端·javascript·react.js
小林攻城狮2 天前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦2 天前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
张元清2 天前
React useIntersectionObserver Hook:懒加载与可见性检测(2026)
javascript·react.js
用户298698530142 天前
在 React 中使用 JavaScript 将 Excel 转换为 PDF
javascript·react.js·webassembly
木木剑光2 天前
我开源了一个 React 组件库,沉淀了多个高频组件和实用 Hooks
前端·javascript·react.js
Csvn2 天前
React 19 `use()` 来了:以后数据加载可以不用 useEffect?
前端·react.js
许我半盏清茶2 天前
前端路由:理解 hash 路由和 history 路由原理
前端·react.js
老王以为3 天前
React Renderer 分离的多平台架构
前端·react native·react.js