前端基础1-6 :es6

点击跳转,详细查看es6

const常量

javascript 复制代码
const LIMIT = 10;
const OBJ_MAP = {
    a: 'A',
    A: 'a'
}

1. 不允许重复声明赋值

js 复制代码
    var arg1 = 'a'
    arg1 = 'aa'

    // 常量 - ES5
    Object.defineProperty(window, 'arg2', {
        value: 'aaa',
        writable: false
    })

    // ES6
    const arg2 = 'aaa'

2. 块级作用域

js 复制代码
    if (true) {
        console.log(arg1)
        var arg1 = 'aa'
    }
    console.log(arg1)

    if (true) {
        const arg1 = 'aa'
    }

    // dead zone => 先声明赋值 再使用

3. const 和 let

js 复制代码
    // 会被改变的对象,到底用const还是let?
    const obj = {
        teacher: 'aa',
        date: '20240530'
    }

    obj.teacher = ''
    const obj2 = obj

    // 1. 栈 + 2. 堆

    // 能用const的地方,都用const

    // 面试:如何对一个对象进行常量化?
    // 破局 - Object.freeze()
    Object.freeze(obj)

    // 追问:只能冻结根层?
    const obj = {
        teacher: 'aa',
        date: '20240530',
        course: {
            course1: 'basic',
            course2: 'trial'
        }
    }

    function deepFreeze(obj) {
        Object.freeze(obj)
        (Object.keys(obj) || []).forEach(key => {
            if (typeof obj[key] === 'object') {
                deepFreeze(obj[key])
            }
        })
    }

deconstruction 解构 - 解开对象结构

js 复制代码
    const obj = {
        teacher: 'yy',
        course: 'es'
    }

    const {
        teacher,
        course
    } = obj;

    const arr = [1, 2, 3, 4, 5]
    const [a, b, c, d, e] = arr;

    let a = 1;
    let b = 2;

    [b, a] = [a, b]

arrow_function 箭头函数

javascript 复制代码
    // 传统函数
    function test(a, b) {
        return a + b;
    }

    const test = function(a, b) {
        return a + b;
    }

    const test = (a, b) => {
        return a + b;
    }
    const test = (a, b) => a + b;
    const test = x => {}

上下文

javascript 复制代码
    const obj2 = {
        teacher: 'aa',
        course: 'es',
        table: ['black', 'red'],
        getTeacher: function() {
            return this.teacher;
        },
        getTable: () => {
            // 不存在独立上下文
        }
    }

class 助力js更加具有面向对象的形式 - 类

js 复制代码
    // 传统类型的对象 - function
    function Course(teacher, course) {
        this.teacher = teacher
        this.course = course
    }

    Course.prototype.getCourse = function() {
        return `teacher is ${this.teacher}`;
    }

    const course = new Course('aa', 'ES')
    const course1 = new Course('aa1', 'ES2')

    // ES6
    class Course {
        constructor(teacher, course) {
            this.teacher = teacher
            this.course = course
        }
        getTeacher() {
            return ''
        }
        static getCourse() {}
    }

    const course = new Course('aa', 'ES')
    course.getTeacher()
    Course.getCourse()

追问

class 的类型是什么?

Function => Object => null

class的prototype

js 复制代码
    Course.prototype

class 函数对象的属性

js 复制代码
    course.hasOwnProperty('teacher')
js 复制代码
// 1. 如何建立只读变量
    class Course {
        constructor(teacher, course) {
            this._teacher = teacher
            let _course = 'es'

            this.getCourse = () => {
                return _course
            }
        }
        // get teacher() {
        //     return this._teacher
        // }
    }

    class Course {
        #course = 'es6'
        set course(val) {
            if (val) {
                this.#course = val
            }
        }
    }

    // 3. 封装核心 - 适配器模式
    // {
    //     name: {
    //         value: 'lodash',
    //         tag: 124,
    //         name: 'es6'
    //     }
    // }
    class utils {
        constructor(core) {
            this._main = core
            this._name = 'myName'
        }
        get name() {
            return {
                ...this._main.name,
                name: `${this._name}`
            }
        }
        set name(val) {
            this._name = val
        }
    }
相关推荐
其美杰布-富贵-李26 分钟前
03 ref、reactive 与 computed 响应式数据
前端·javascript·vue.js
OpenTiny社区28 分钟前
GenUI SDK v1.3.0 发布|多框架兼容,一键换物料,渲染器 & 演练场全面增强!
前端·ai编程
用户9385156350730 分钟前
useRef + Web Worker 实战:React 如何优雅地拥抱多线程
前端·javascript·react.js
他们叫我秃子1 小时前
前端开发转 Go 全栈(五):终于遇到熟人了,Go 的闭包和高阶函数原来这么像 JavaScript
前端·后端·go
用户61595868000221 小时前
从 0 写一个迷你 wujie:用原生 Web API 实现微前端
前端
妙码生花1 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(五十七):AI时代对组件封装的新理解、agInput 统一入口,动态表单预备
前端·后端·go
fairyly1 小时前
接手新项目不用硬啃源码,我的 TRAE Work 快速上手实战工作流
前端·ai编程·trae
搬砖记录员1 小时前
用 PyAutoGUI 手搓录屏"老板键":从原理到四套可跑源码,附工程化落地思考
前端
windliang1 小时前
Claude Code 源码分析(七):Skill 如何进入 Agent
前端·人工智能·面试
栀鸢ouo1 小时前
《Vue 3 实现多主题切换和自定义主题》
前端