当前位置:网站首页>JS class is not just a simple syntax sugar!
JS class is not just a simple syntax sugar!
2022-06-28 12:49:00 【51CTO】
Long ago , Wrote an article “ class ” Design patterns and “ Prototype ” Design patterns ——“ Copy ” and “ entrust ” The difference of , That is to say : Code reuse , That is inheritance 、 rewrite , There are two ways of thinking :1. Object oriented class inheritance ;2. be based on JavaScript Prototype inheritance of prototype chain ; The main feature of the former is : Copy , Generally speaking, it means to put variables 、 Attribute copy again , The main feature of the latter is : entrust , It is realized through the search of attributes .
Later on , Deepen understanding JavaScript Inheritance in advanced programming , Including constructor inheritance 、 Prototype inheritance 、 Combination inheritance 、 Parasitic combination inheritance , Each has its own shortcomings .
also , Ben Gua especially remembers : Wiki pair JavaScript An explanation of the origin
JavaScript The language design of is mainly influenced by Self( A prototype based programming language ) and Scheme( A functional programming language ) Influence . In grammatical structure, it is similar to C Language There are many similarities .
Last , My summary is :JavaScript The design itself is “ Through prototype delegation ” To realize code reuse , result ES6 We've come up with a class As a grammar sugar , It is still based on the prototype chain , But it is to realize object-oriented , Object oriented is based on class Kind of “ Copy ” To achieve code reuse .
class and Prototype , It's two different things ,JS class Mix the two together , Don't be awkward ?
Later I saw some articles saying that JS Use in class Classes cause some problems , Such as this one : medium.com/giant-machi…, So be more determined to reduce the use of class .
But in fact , The title of this article is :JS class It's not just a simple grammar sugar , therefore , This article is not meant to say that it is bad , But to say it's good !
Come on , Spread your wings !
class The first one is good : Private variables
If not class , Is there any more elegant way to implement the private variables of the following subclasses ?
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
Envisaged , We use the idea of prototype chain to simulate ( object ):
const Person = {
set givenName(givenName) {
this._givenName = givenName;
},
set familyName(familyName) {
this._familyName = familyName;
},
get fullName() {
return `${this._givenName} ${this._familyName}`;
}
};
const test = Person; // Let's assume that object simulation class
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
No private property implemented _givenName
and class You can save values as private , So that the object cannot be modified outside :
Code examples can refer to : javascript-classes-are-not-just-syntactic-sugar
class The second one is good :super Inherit
class Can pass super More elegant implementation of inheritance 、 And rewrite , such as :
If it is the same , Prototype chain , It might look something like this :
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
It's a bit confusing ,this Pointing to and fro , And you can do it manually in the constructor assign operation , This increases code execution time .
To sum up, two points ,JS class It is very valuable to use it , Don't run away , Use it in the right situation , You will find its charm ~~
OK, The above is the sharing of this article . Like, follow the comments , Help good writing
I'm Anthony nuggets 100 Popular front-end technology bloggers with a reading volume of 10000 INFP Writing personality persistence 1000 Japanese Geng Wen Pay attention to me , Spend the long programming years with you
边栏推荐
- 一文搞懂leveldb写操作
- 分页样式 flex设置成在尾部显示(即使页数加长 也不会因为在末尾而换行)
- async-validator.js数据校验器
- ASP.NET CORE Study02
- unity发布 webgl在手机端 inputfield唤醒键盘输入
- 深入理解贝叶斯定理
- Tips for using ugui (V) using scroll rect component
- Which securities company is the best and safest? How to open an account is the safest
- 2022招商FinTech比赛总结
- 易观分析《2022年中国银行业隐私计算平台供应商实力矩阵分析》研究报告正式启动
猜你喜欢
随机推荐
性能测试-01-简介
Hundreds of lines of code to implement a JSON parser
Matplotlib_Study01
一种跳板机的实现思路
JS class 并不只是简单的语法糖!
centos6.5 php+mysql mysql库找不到
My NVIDIA developer tour -jetson nano 2GB teaches you how to train models (complete model training routines)
10万美元AI竞赛:寻找大模型做得“更烂”的任务
Jerry's wif interferes with Bluetooth [chapter]
我的NVIDIA开发者之旅-Jetson Nano 2gb教你怎么训练模型(完整的模型训练套路)
[MySQL from introduction to mastery] [advanced part] (III) creation of MySQL users_ Modification_ Delete and password settings
微信授权登陆
杰理之SPI1外挂FLASH录音修改【篇】
Siyuan official paid synchronization Guide
分页样式 flex设置成在尾部显示(即使页数加长 也不会因为在末尾而换行)
杰理之wif 干扰蓝牙【篇】
newest! Introduction and practical tutorial of point cloud processing based on open3d
Jerry's wif interferes with Bluetooth [chapter]
It really doesn't matter if a woman fails to pass the college entrance examination and buys thousands of villas in a counter attack
[unity Editor Extension Foundation], editorguilayout (I)






