javascript-Class-类 创建时间:2020-09-19 21:54:36
写在前面 其实javascript的class内容比较简单,如果学习过其他面向对象语言的话,基本上只要浏览一遍就可掌握,所以只做简单罗列
参考资料
https://es6.ruanyifeng.com/#docs/class
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes
https://github.com/lin-123/javascript
正篇 JavaScript 语言中,生成实例对象的传统方法是通过构造函数,但和跟传统的面向对象语言略有差距,所以es6新加入了class,但其实大部分功能es5也能实现
1 2 3 4 5 6 7 8 9 10 11 function Point (x, y) { this .x = x; this .y = y; } Point.prototype.toString = function () { return '(' + this .x + ', ' + this .y + ')' ; }; var p = new Point(1 , 2 );
1 2 3 4 5 6 7 8 9 10 11 12 13 class Point { constructor (x, y) { this .x = x; this .y = y; } toString() { return '(' + this .x + ', ' + this .y + ')' ; } } var p = new Point(1 , 2 )console .log(p.toString())
1 2 3 4 console .log(typeof (Point)) console .log(Point === Point.prototype.constructor)
上面基本说明了js类
构造方法/函数
1 2 3 4 5 6 7 8 class Harekaze { constructor () { console .log("init" ) } } var p = new Harekaze()
get set 方法 1 2 3 4 5 6 7 8 9 10 11 12 13 class Rectangle { constructor (height, width) { this .height = height; this .width = width; } } const square = new Rectangle(10 , 5 );console .log(square.height);square.height = 100 console .log(square.height);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Rectangle { constructor () { } get area() { return this .calcArea() } calcArea() { return this .height * this .width; } } const square = new Rectangle();square.height = 10 square.width = 10 console .log(square.area);
js中的class自带get、set方法
原型和静态方法 原型方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Rectangle { constructor (height, width) { this .height = height || 0 this .width = width || 0 } get area() { return this .calcArea() } calcArea() { return this .height * this .width; } } const square = new Rectangle();console .log(square.area);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Rectangle { constructor (height, width) { this .height = height || 0 this .width = width || 0 } area() { return this .height * this .width; } } const square = new Rectangle();console .log(square.area());
静态方法 静态方法不需要实例化就可以直接使用
1 2 3 4 5 6 7 8 9 10 11 12 class Rectangle { constructor() { } static area (height, width) { return height * width; } } console.log(Rectangle.area(2 , 3 ));
私有属性 和 私有方法 私有属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class IncreasingCounter { #count = 0; get value() { console .log('Getting the current value!' ); return this.#count; } increment() { this.#count++; } } var test = new IncreasingCounter()console .log(test.count)console .log(test.value)
私有方法 注:我用的为nodejs v12.9.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
将私有方法写在类外,但这样只有其他文件应用该类是才有作用1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var sum = function (a, b ) { return a + b } module .exports = class Foo { #a; #b; constructor (a, b) { this.#a = a; this.#b = b; } printSum() { console.log(sum(this.#a, this.#b)); } }
还有就是使用Symbol
继承 javascript
的继承会继承父类的所有属性和方法
1 2 3 4 5 6 7 8 9 10 11 class Harekaze {} class Akeno extends Harekaze { constructor (){ super () } } var x= new Akeno()