js拾遗--map

js拾遗--map

map 函数的第二个参数

map 可以传递第二个参数,第二个参数的值会赋值给当前 this

js
const lesson = {
    name: "课程",
    lessons: ["js", "ts", "vue"],
    show() {
        return this.lessons.map(function (lesson) {
            return this.name + "-" + lesson
        }, this)
    },
}
console.log(lesson.show())

当然,我们可以使用箭头函数

箭头函数的 this 就是函数执行上下文 - 父级作用域的 this

js
const lesson = {
    name: "课程",
    lessons: ["js", "ts", "vue"],
    show() {
        return this.lessons.map(lesson => this.name + "-" + lesson)
    },
}
console.log(lesson.show())
degit -- 拉取项目(不包含提交记录)
大同博物馆游览留念

评论区

评论加载中...