ES6基础-ES6 class

我会带着你远行 2021-08-20 00:23 617阅读 0赞

file

作者 | Jeskson

来源 | 达达前端小酒馆

ES - Class

类和面向对象:

面向对象,即万物皆对象,面向对象是我们做开发一种的方式,开发思维,面向对象的思维中万物皆对象,以人作为例子,它的特性有哪些。比如有姓名,性别,出生年月,身高等,还有人的行为,为吃饭,睡觉。特性和行为组合起来就成为人类,特性和行为都是人都有的,通过这些不同的特性和行为给不同的值,构成不同的人。

使用类进行编程,是可以降低维护成本,类的封装性是非常强的,很多情况下,类和业务是低耦合,使用类可以让代码高度复用,类是具有继承的特性的,所以类需要扩充,是不需要修改自身的,就可进行扩展,类的使用降低了设计成本,使用简单。

那么什么是类与对象,讲解ES6中类的特性,类的继承,Babel,基于流程控制的形变类实现。

什么是类与对象以及它们之间的关系

封装的思想

  1. (function() {
  2. let snake = []; // 存放
  3. let food = { x: 0, y: 0 }; // 食物
  4. function move() {
  5. // 移动
  6. }
  7. function getFood() {
  8. // 是否吃到食物
  9. }
  10. function putFood() {
  11. // 放置食物
  12. }
  13. function gameover() {
  14. // 判断游戏结束
  15. }
  16. function init() {
  17. // 入口函数
  18. // 初始化
  19. }
  20. start();
  21. })();
  22. class Car {
  23. // 构造函数
  24. constructor(...args) {
  25. console.log(args);
  26. }
  27. }
  28. new Car('蓝色', 2)
  29. class Car {
  30. constructor(wheel, color, length, width) {
  31. this.whell = wheel;
  32. this.color = color;
  33. this.length = length;
  34. this.width = width;
  35. this.speed = 0; // 实速
  36. }
  37. // 加速
  38. speedUp() {
  39. this.speed = 1;
  40. }
  41. }
  42. const car = new Car(2, '#000', 23, 45);
  43. console.log(car.color);
  44. console.log(car.spedd);
  45. car.speedUp(); // 加速
  46. console.log(car);

三大基本特性:多态,继承,封装。

多态,同一个接口,有不同的表现。

音乐播放器类

  1. class AudioPlayer {
  2. constructor(container) {
  3. this.container = document.querySelector(container);
  4. this.songsList = []; // 歌单列表
  5. this.dom = null; // 用于存放dom
  6. this.audio = new Audio();
  7. this.status = 0;
  8. this.getSongs();
  9. this.createElement();
  10. this.bindEvents();
  11. this.render();
  12. }
  13. getSongs() {
  14. this.songsList = [
  15. {
  16. cover: '',
  17. url: .mp3,
  18. singer: {},
  19. name: ''
  20. }
  21. ];
  22. }
  23. createElement() {
  24. const div = document.createElement('div');
  25. div.innerHTML = `
  26. <div>播放按钮</div>
  27. <div>进度条</div>
  28. `
  29. this.dom = div;
  30. }
  31. bindEvents() {
  32. this.div.querySelector('.btn').addEventListener('click',()=>{
  33. console.log('开始播放');
  34. })
  35. }
  36. render() {
  37. this.container.appendChild(this.dom);
  38. }
  39. }

静态方法与静态属性

静态属性和静态方法,getter与setter,类的表达式,name属性与New.target属性,es5中模拟类。

类中的静态属性与静态方法,有两个特点:

不会被类的实例所拥有的属性和方法,只有类自身拥有;只能通过类调用。

用static关键字去声明一个静态方法

  1. class Car {
  2. static totalCar = 0;
  3. constructor() {
  4. this.speed = 0;
  5. this.errors = 0;
  6. }
  7. speedUp() {
  8. this.speed = 1;
  9. }
  10. // 自检
  11. check() {
  12. console.log('开始');
  13. if(this.errors === 0){
  14. console.log('this');
  15. }
  16. }
  17. // 工厂
  18. static checker() {
  19. console.log('haha');
  20. }
  21. static repair(car) {
  22. console.log('da');
  23. }
  24. }
  25. const car = new Car();
  26. car.checker();
  27. Car.repair(car);
  28. Car.repair('1号车');
  29. Car.属性名 = 属性值;
  30. class Person {
  31. }
  32. Person.format = programmer => {
  33. programmer.haveGirlFriend = true;
  34. programmer.hair = true;
  35. };
  36. class Programmer {
  37. constructor() {
  38. this.haveGirlFriend = false;
  39. this.hair = false;
  40. }
  41. }
  42. const programmer = new Programmer();
  43. console.log(programmer);

类表达式:

  1. const Person = class P {
  2. constructor() {
  3. console.log('dada');
  4. }
  5. }
  6. new Person();

函数表达式:

  1. const a = function() {
  2. }

函数声明:

  1. function a() {
  2. }

getter与setter

getter,setter类似于给属性提供钩子在获取属性值和设置属性值的时候做一些额外的事情

ES5 getter/setter

在对象字面量中书写get/set方法Object.definedProperty

  1. const obj = {
  2. _name: '',
  3. get name() {
  4. return this._name;
  5. },
  6. set name(val) {
  7. this._name = val;
  8. }
  9. }
  10. obj.name = 3;

Object.definedProperty

  1. var obj = {
  2. _name: ''
  3. };
  4. Object.definedProperty(obj, 'age', {
  5. value: 12,
  6. enumerable: true
  7. });
  8. var i;
  9. for(i in obj) {
  10. console.log(i);
  11. }
  12. console.log(obj);
  13. var obj = {
  14. _name: ''
  15. };
  16. Object.defineProperty(obj, 'name', {
  17. get: function() {
  18. console.log('正在访问name');
  19. },
  20. set: function(val) {
  21. console.log('正在修改');
  22. this._name = val;
  23. }
  24. });
  25. class Person {
  26. constructor() {
  27. this._name = '';
  28. }
  29. get name() {
  30. console.log('getname);
  31. return `名字${this._name}`;
  32. }
  33. set name(val) {
  34. console.log('name');
  35. this._name = val;
  36. }
  37. }
  38. const person = new Person();
  39. person.name = '';
  40. console.log(person.name);
  41. class AudioPlayer {
  42. constructor() {
  43. this._status = 0;
  44. this.status = 0;
  45. }
  46. get status() {
  47. return this._status;
  48. }
  49. set status(val) {
  50. const StatusMap = {
  51. 0: '暂停',
  52. 1: '播放',
  53. 2: '加载中'
  54. };
  55. document.querySelector('#app. play-btn').innerText = StatusMap[val];
  56. this._status = val;
  57. }
  58. }

name属性与new.target属性

  1. class Person {
  2. }
  3. console.log(Person.name);
  4. const da = class d {
  5. }
  6. console.log(da.name);
  7. class Car {
  8. constructor() {
  9. console.log(new.target);
  10. }
  11. }
  12. new Car();
  13. function Car() {
  14. if(new target !== Car) {
  15. throw Error('使用new调用car');
  16. }
  17. }
  18. new Car();
  19. function Car() {
  20. if(!(this instanceof Car)) {
  21. throw Error('new');
  22. }
  23. }
  24. new Car();

使用ES5模拟类

  1. // 构造函数
  2. class Car {
  3. constructor() {
  4. }
  5. }
  6. function Car() {
  7. }
  8. new Car();
  9. function Person(name, age) {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. new Person('张三', 12);

创建一个空的对象,把构造函数的prototype属性作为空对象的原型,this赋值为这个空对象,执行函数,如果函数没有返回值,则返回this

  1. function Constructor(fn, args) {
  2. var _this = Object.create(fn.prototype);
  3. fn.apply(_this, args);
  4. }
  5. function Constructor(fn, args) {
  6. var _this = Object.create(fn.prototype);
  7. var res = fn.apply(_this, args);
  8. return res ? res : _this;
  9. }
  10. function Person(name, age) {
  11. this.name = name;
  12. this.age = age;
  13. }
  14. Person.prototype.say = function() {
  15. console.log('dada' this.name);
  16. }
  17. var person = Constructor(Person, ['da', 12]);
  18. console.log(person);
  19. // 重载
  20. class SimpleCalc {
  21. addCalc(...args) {
  22. if(args.length === 0) {
  23. return this.zero();
  24. }
  25. if(args.length === 1){
  26. return this.onlyOneArgument(args);
  27. }
  28. return this.add(args);
  29. }
  30. zero() {
  31. return 0;
  32. }
  33. onlyOneArgument() {
  34. return args[0];
  35. }
  36. add(args) {
  37. return args.reduce((a,b) => a b,0);
  38. }
  39. }
  40. function post(url, header, params) {
  41. if(!params) {
  42. params = header;
  43. header = null; // undefined
  44. }
  45. }
  46. post('http:...' , {
  47. a:1,
  48. b:2
  49. });

ES5中的继承

  1. // 利用构造函数
  2. function P() {
  3. this.name = 'parent';
  4. this.gender = 2;
  5. this.say = function() {
  6. console.log('ddd');
  7. }
  8. }
  9. P.prototype.test = function() {
  10. console.log('da');
  11. }
  12. function C() {
  13. P.call(this);
  14. this.name = 'child',
  15. this.age = 11;
  16. }
  17. C.prototype = new P();
  18. var child = new C();
  19. child.say();

Babel是一个JavaScript编译器

file

  1. const add = (a,b) => a b;
  2. alert(add(1,2));
  3. alert(add(3,4));
  4. class Person {
  5. static aa = 1;
  6. bb = 2;
  7. static A() {
  8. alert('b');
  9. }
  10. constructor() {
  11. }
  12. }
  13. class Car {
  14. static total_car = 0;
  15. color='#000';
  16. constructor(color) {
  17. Car.total_car = 1;
  18. this.color = color;
  19. }
  20. }
  21. new Car();
  22. new Car();
  23. new Car();
  24. console.log(Car.total_car);

❤️ 不要忘记留下你学习的脚印 [点赞 收藏 评论]

作者Info:

【作者】:Jeskson

【原创公众号】:达达前端小酒馆。

【转载说明】:转载请说明出处,谢谢合作!~

关于目前文章内容即涉及前端,PHP知识点,如果有兴趣即可关注,很荣幸,能被您发现,真是慧眼识英!也感谢您的关注,在未来的日子里,希望能够一直默默的支持我,我也会努力写出更多优秀的作品。我们一起成长,从零基础学编程,将 Web前端领域、数据结构与算法、网络原理等通俗易懂的呈现给小伙伴。分享 Web 前端相关的技术文章、工具资源、精选课程、热点资讯。

#

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。

#

请点赞!因为你们的赞同/鼓励是我写作的最大动力!

欢迎关注达达的CSDN!

这是一个有质量,有态度的博客

7d927f18ebd05ea1d505a572393fbc87.jpg

发表评论

表情:
评论列表 (有 0 条评论,617人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ES6Class

    Class 的静态属性和实例属性 静态属性指的是 Class 本身的属性,即`Class.propName`,而不是定义在实例对象(`this`)上的属性。 c

    相关 ES6--Class

    Class概述 概述 在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。 class 的本质是 function。 它可以