ES6的class

ES6 的类,可以看作构造函数的另一种写法。

javascript 复制代码
function Parent(name) {
    this.name = name
}

Parent.prototype.toString = function() {
  return `this is ${this.name}`
}

let parent = new Parent('zhangsan') 
console.log(parent.toString())  // this is zhangsan
javascript 复制代码
class Parent {
    constructor(name) {
      this.name = name
    }
    
    toString() {
      return `this is ${this.name}`
    }
}

let parent = new Parent('zhangsan')
console.log(parent.toString())  // this is zhangsan

typeof Parent // "function"
Parent === Parent.prototype.constructor  // true

类的数据类型就是函数,类本身就指向构造函数。

构造函数的prototype属性在ES6的类上面继续存在,定义在类内部的函数相当于定义在类的prototype属性上,prototype对象的constructor属性,直接指向"类"的本身, 但类内部定义的方法不能枚举

在一个方法前,加上static 关键字,表示该方法不会被实例继承 ,而是直接通过类来调用 ,这就称为**"静态方法"**,静态方法包含this关键字,this指的是类,而不是实例

相关推荐
ZC跨境爬虫34 分钟前
跟着 MDN 学 HTML day_52:(深入 XPathExpression 接口)
开发语言·前端·javascript·ui·html·音视频
不会写DN1 小时前
通过白名单解决 pnpm i 报错 Ignored build scripts
javascript·面试·npm
UXbot1 小时前
AI 原型工具零设计基础操作指南与功能解析(2026)
前端·ui·产品经理·原型模式·web app
布局呆星2 小时前
Vue Router 笔记(二):正则路由、组件通信与动态路由
前端·javascript·vue.js
rising start2 小时前
InsightEdu - 轻量智能学习平台
javascript·axios·css3·html5·fastapi·orm·dify
qq_381338503 小时前
前端虚拟列表与无限滚动性能优化实战:从万级数据到丝滑体验
前端·javascript·html·优化
hexu_blog3 小时前
前端vue后端springboot如何实现图片格式转换
前端·javascript·vue.js
代码煮茶3 小时前
Vue3 项目规范实战 | ESLint+Prettier+Git Hooks 搭建前端代码规范体系
前端·javascript·vue.js
hexu_blog4 小时前
前端vue 后端springboot如何实现图片去水印
前端·javascript·vue.js
whuhewei4 小时前
React搜索框组件
前端·javascript·react.js