JavaScript的class语法糖内容有哪些
Admin 2022-08-12 群英技术资讯 532 次浏览
后来呢,深入了解 JavaScript 高级程序设计中的继承,包括构造函数继承、原型继承、组合继承、寄生组合继承,都有各自的缺点,有兴趣的朋友,可以看我这篇文章。
还有,本瓜特别记住:维基对 JavaScript 起源的解释
JavaScript的语言设计主要受到了Self(一种基于原型的编程语言)和Scheme(一门函数式编程语言)的影响。在语法结构上它又与C语言有很多相似。
最后,我的小结呢就是:JavaScript 本身的设计就是“通过原型委托”来实现代码复用的,结果 ES6 搞出了个 class 作为语法糖,其本身还是基于原型链,但又是为了实现面向对象,面向对象是基于 class 类那种“复制”来实现代码复用。
类 和 原型,是两种不同的东西,JS class 将二者混在了一起,别不别扭?
后来也看到一些文章说在 JS 中使用 class 类会造成一些困扰,所以更加坚定要减少使用 class 。
而实际上,本篇题目是:JS class 并不只是简单的语法糖,所以,本篇并不是为了说它不好,而是要说它的好的!
来吧,展翅!
如果不用 class , 还有什么更优雅的方法实现以下子类的私有变量吗?
class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } // Person.constructor get FullName () { return this.firstName + " " + this.lastName; } } // Person class Employee extends Person { #salary; constructor(firstName, lastName, salary) { super(firstName, lastName); this.salary = salary; } get salary() { return this.#salary; } set salary(salary) { this.#salary = salary; console.log("Salary changed for " + this.fullName + " : $" + this.salary); } } // Employee
设想下,我们用原型链的思路模拟(对象):
const Person = { set givenName(givenName) { this._givenName = givenName; }, set familyName(familyName) { this._familyName = familyName; }, get fullName() { return `${this._givenName} ${this._familyName}`; } }; const test = Person; // 这里假设用 对象 模拟 类 test.givenName = "Joe"; test.familyName = "Martinez"; console.log("test.fullName", test.fullName); // Joe Martinez console.log("test.givenName", test.givenName); // undefined console.log("test._givenName", test._givenName); // Joe
没有实现私有属性 _givenName
而 class 可以将值存为私有,使得对象外部不能修改:
class 可以通过 super 更优雅的实现继承、和重写,比如:
class Cash { constructor() { this.total = 0; } add(amount) { this.total += amount; if (this.total < 0) this.total = 0; } } // Cash class Nickles extends Cash { add(amount) { super.add(amount * 5); } } // Nickles
如果是按照老样子,原型链,它可能是这样的:
const Cash = function() { this.total = 0; }; // Cash Cash.prototype = { add : function(amount) { this.total += amount; if (this.total < 0) this.total = 0; } }; // Cash.prototype const Nickles = function() { Object.assign(this, new Cash()); this.add = function(amount) { Cash.add.apply(this, amount); }; } // Nickles
读起来有点乱,this 指来指去,还有在构造函数中手动做的 assign 操作,这会增加代码执行耗时。
综上两点,JS class 还是非常有使用它的价值的,不用逃避,把它用在合适的场景,肯定会发现其魅力~~
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了uni-app中的样式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
本文主要讲解了如何在vue中使用pdfjs预览pdf文件,这样的优势是无须让用户安装专门的软件即可实现预览,下面就看看如何实现这个需求
jQuery中让li标签显示与隐藏的方法是什么?li标签用于定义列表项目,有时候我们需要对其做显示或隐藏的操作,那么有什么方法可以实现呢?对此本文给大家分享两种方法,感兴趣的朋友就继续往下看吧。
本文主要介绍了react拖拽组件react-sortable-hoc的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
async与await捕捉错误正常的输出时try catch捕捉错误多个异步嵌套时 await-to-js异步嵌套使用了try,代码相对不够智能总结async与await捕捉错误正常的输出
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008