function,对象和class

2022-03-23  本文已影响0人  申_9a33

含义


1.普通方法和属性

class

class Person1 {
  constructor(name){
    this.name = name
  }

  play(){
    console.log(`${this.name} 在打游戏`)
  }
}

const p1 = new Person1('德玛')
console.log(p1)
console.log(p1.name)
p1.play()

function

const Person2 = function(name){
  this.name = name
  this.play = function(){
    console.log(`${this.name} 在打游戏`)
  }
}
const p2 = new Person2('诺手')
console.log(p2)
console.log(p2.name)
p2.play()

2. 静态方法和属性

class

class Person1 {
  static nickName

  static play(){
    console.log(`${this.nickName} 在打游戏`)
  }
}
Person1.nickName = '德玛西亚'
Person1.play()

const p1 = new Person1()
console.log(p1)

function

const Person2 = function(){
}
Person2.nickName = '洛克萨斯'
Person2.play = function(){
  console.log(`${this.nickName} 在打游戏`)
}
Person2.play()

const p2 = new Person2()
console.log(p2)

3.继承

继承使用

class Person {
  constructor(name){
    this.name = name
  }

  eat(){
    console.log(`${this.name} 在吃东西`)
  }

  play(){
    console.log(`${this.name} 在打游戏`)
  }
}

const p = new Person('德玛西亚')
p.eat()
p.play()

// 继承 Person
class Man extends Person {
  constructor(name){
    super(name)
  }

  // 重写play
  play(){
    console.log(`${this.name} 再打英雄联盟`)
  }
}
const m = new Man('洛克萨斯')
m.eat()
m.play()

class Son extends Man {
  constructor(name){
    super(name)
  }

  sleep(){
    console.log(`${this.name} 在睡觉`)
  }
}

const s = new Son('儿子')
s.eat()
s.play()
s.sleep()

原理(使用bebal 编译上述代码)

"use strict";

function _inheritsLoose(subClass, superClass) { 
   subClass.prototype = Object.create(superClass.prototype);
   subClass.prototype.constructor = subClass; 
   _setPrototypeOf(subClass, superClass); 
}

function _setPrototypeOf(o, p) { 
  _setPrototypeOf = Object.setPrototypeOf  || 
  function _setPrototypeOf(o, p) {
     o.__proto__ = p; 
    return o; 
  };
   return _setPrototypeOf(o, p); 
}

var Person = /*#__PURE__*/function () {
  function Person(name) {
    this.name = name;
  }

  var _proto = Person.prototype;

  _proto.eat = function eat() {
    console.log(this.name + " \u5728\u5403\u4E1C\u897F");
  };

  _proto.play = function play() {
    console.log(this.name + " \u5728\u6253\u6E38\u620F");
  };

  return Person;
}();

var p = new Person('德玛西亚');
p.eat();
p.play(); // 继承 Person

var Man = /*#__PURE__*/function (_Person) {
  _inheritsLoose(Man, _Person);

  function Man(name) {
    return _Person.call(this, name) || this;
  } // 重写play


  var _proto2 = Man.prototype;

  _proto2.play = function play() {
    console.log(this.name + " \u518D\u6253\u82F1\u96C4\u8054\u76DF");
  };

  return Man;
}(Person);

var m = new Man('洛克萨斯');
m.eat();
m.play();

var Son = /*#__PURE__*/function (_Man) {
  _inheritsLoose(Son, _Man);

  function Son(name) {
    return _Man.call(this, name) || this;
  }

  var _proto3 = Son.prototype;

  _proto3.sleep = function sleep() {
    console.log(this.name + " \u5728\u7761\u89C9");
  };

  return Son;
}(Man);

var s = new Son('儿子');
s.eat();
s.play();
s.sleep();

函数实现类,使用匿名函数,返回构造函数

var Person = /*#__PURE__*/function () {
  function Person(name) {
    this.name = name;
  }

  var _proto = Person.prototype;

  _proto.eat = function eat() {
    console.log(this.name + " \u5728\u5403\u4E1C\u897F");
  };

  _proto.play = function play() {
    console.log(this.name + " \u5728\u6253\u6E38\u620F");
  };

  return Person;
}();

函数实现继承

var Man = /*#__PURE__*/function (_Person) {
  _inheritsLoose(Man, _Person);

  function Man(name) {
    return _Person.call(this, name) || this;
  } // 重写play


  var _proto2 = Man.prototype;

  _proto2.play = function play() {
    console.log(this.name + " \u518D\u6253\u82F1\u96C4\u8054\u76DF");
  };

  return Man;
}(Person);
上一篇下一篇

猜你喜欢

热点阅读