Es2015 传统方法创建类


<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
      <strong>We're sorry but es6test doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    <div id="app"></div>
    <script type="module">
      // 传统js创建类
      function Person(name,age){ = name;
        this.age = age;
      Person.prototype.showName = function (){
        return "名字为:" +;
      Person.prototype.showAge = function (){
        return "年龄为:" + this.age;
      let p1 = new Person('星哥',18);
      console.log(p1.showName()); // 名字为:星哥
      console.log(p1.showAge());  // 年龄为:18

      // Object.assign添加对象方法
          return + "说话";
          return + "吃饭";
      console.log(p1.speak());  // 星哥说话
      console.log(;    // 星哥吃饭

    <!-- built files will be auto injected -->
Last modification:June 24th, 2019 at 09:56 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment