秋雨
首页
  • HTML
  • CSS
  • JavaScript
设计模式
webpack
  • 前端常识
  • ES
  • React
  • Redux
  • ReactRouter
  • 事件循环
  • 浏览器渲染流程
  • Vue项目性能优化
  • Vue
  • App
Github
首页
  • HTML
  • CSS
  • JavaScript
设计模式
webpack
  • 前端常识
  • ES
  • React
  • Redux
  • ReactRouter
  • 事件循环
  • 浏览器渲染流程
  • Vue项目性能优化
  • Vue
  • App
Github
  • 构造器模式
  • 原型链模式
  • 工厂模式
  • 抽象工厂模式
  • 建造者模式
  • 单例模式
  • 装饰器模式
  • 适配器模式
  • 适配器模式
  • 代理模式
  • 观察者模式
  • 发布订阅模式
  • 模块模式
  • 桥接模式
  • 组合模式
  • 命令模式
  • 命令模式
  • 迭代器模式
  • 职责链模式

ES5

var employee1 = {
  name: "kerwin",
  age: 100,
};

var employee2 = {
  name: "tiechui",
  age: 100,
};
function Employee(name, age) {
  this.name = name;
  this.age = age;
}

Employee.prototype.say = function () {
  console.log(this.name + "_" + this.age);
};

var employee1 = new Employee("kerwin", 100);
var employee2 = new Employee("tiechui", 10);

ES6

class Employee {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  say() {
    console.log(this.name + "_" + this.age);
  }
}

var employee1 = new Employee("kerwin", 100);
console.log(employee1);
employee1.sqy();

// 在class中, say方法是挂载到原型上的。
最后更新:
贡献者: Mrlishizhen
Prev
构造器模式
Next
工厂模式