码迷,mamicode.com
首页 > 其他好文 > 详细

typescript继承 __extends = (this && this.__extends) || (function () { 代码解读

时间:2020-07-21 23:17:21      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:cal   class   obj   OLE   string   属性   原型   height   设置   

    // function (d, b) 参数d子函数,b父函数. 
   // (this && this.__extends)防止this.extends取到空类型, 如果this类型为空, 不执行this.__extends.
1
  var __extends = (this && this.__extends) || (function () { 2 var extendStatics = function (d, b) { 3 extendStatics = Object.setPrototypeOf ||
         // 设置__proto__
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
         //拷贝静态属性
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; 6 return extendStatics(d, b); 7 }; 8 return function (d, b) {
      // 拷贝静态属性
9 extendStatics(d, b);
      //拷贝静态函数
10 function __() { this.constructor = d; }
      //设置原型
11 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 12 }; 13 })();

 

TS源文件:

 1 class Shape {
 2  
 3     area: number;
 4     color: string;
 5     text: string;
 6  
 7     constructor (public name: string, width: number, height: number ) {
 8         this.area = width * height;
 9         this.color = "pink";
10         this.text = ‘I am a 2D object:‘;
11     };
12  
13     shoutout() {
14         return this.text + this.color + " " + this.name +  " with an area of " + this.area + " cm squared.";
15     }
16 }
17 
18 class Shape3D extends Shape {
19  
20     volume: number;
21     
22  
23     constructor ( public name: string, width: number, height: number, length: number ) {
24         super( name, width, height );
25         this.volume = length * this.area;
26         this.text = ‘I am a 3D object:‘;
27     };
28  
29     shoutout() {
30         return this.text + this.name +  " with a volume of " + this.volume + " cm cube.";
31     }
32  
33     superShout() {
34         return super.shoutout();
35     }
36 }
37 
38 class Shape4D extends Shape3D {
39     
40     d4volume: number;
41     constructor ( public name: string, width: number, height: number, length: number, d4: number ){
42         super(name, width, height, length);
43         this.d4volume = d4 * this.volume;
44         this.text = ‘I am a 4D object‘;
45     };
46     
47     shoutout() {
48         return this.text + this.name +  " with a d4volume of " + this.d4volume + " cm d4cube.";
49     }
50     
51     superShout(){
52         return super.shoutout();
53     }
54 }
55 
56 var cube = new Shape3D("cube", 30, 30, 30);
57 console.log( cube.shoutout() );
58 console.log( cube.superShout());
59 
60 var d4cube = new Shape4D("d4cube", 30, 30, 30, 30);
61 console.log( d4cube.shoutout() );
62 console.log( d4cube.superShout() );

 

js编译后的文件:

 1 var __extends = (this && this.__extends) || (function () {
 2     var extendStatics = function (d, b) {
 3         extendStatics = Object.setPrototypeOf ||
 4             ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
 5             function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
 6         return extendStatics(d, b);
 7     };
 8     return function (d, b) {
 9         extendStatics(d, b);
10         function __() { this.constructor = d; }
11         d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12     };
13 })();
14 var Shape = /** @class */ (function () {
15     function Shape(name, width, height) {
16         this.name = name;
17         this.area = width * height;
18         this.color = "pink";
19         this.text = ‘I am a 2D object:‘;
20     }
21     ;
22     Shape.prototype.shoutout = function () {
23         return this.text + this.color + " " + this.name + " with an area of " + this.area + " cm squared.";
24     };
25     return Shape;
26 }());
27 var Shape3D = /** @class */ (function (_super) {
28     __extends(Shape3D, _super);
29     function Shape3D(name, width, height, length) {
30         var _this = _super.call(this, name, width, height) || this;
31         _this.name = name;
32         _this.volume = length * _this.area;
33         _this.text = ‘I am a 3D object:‘;
34         return _this;
35     }
36     ;
37     Shape3D.prototype.shoutout = function () {
38         return this.text + this.name + " with a volume of " + this.volume + " cm cube.";
39     };
40     Shape3D.prototype.superShout = function () {
41         return _super.prototype.shoutout.call(this);
42     };
43     return Shape3D;
44 }(Shape));
45 var Shape4D = /** @class */ (function (_super) {
46     __extends(Shape4D, _super);
47     function Shape4D(name, width, height, length, d4) {
48         var _this = _super.call(this, name, width, height, length) || this;
49         _this.name = name;
50         _this.d4volume = d4 * _this.volume;
51         _this.text = ‘I am a 4D object‘;
52         return _this;
53     }
54     ;
55     Shape4D.prototype.shoutout = function () {
56         return this.text + this.name + " with a d4volume of " + this.d4volume + " cm d4cube.";
57     };
58     Shape4D.prototype.superShout = function () {
59         return _super.prototype.shoutout.call(this);
60     };
61     return Shape4D;
62 }(Shape3D));
63 var cube = new Shape3D("cube", 30, 30, 30);
64 console.log(cube.shoutout());
65 console.log(cube.superShout());
66 var d4cube = new Shape4D("d4cube", 30, 30, 30, 30);
67 console.log(d4cube.shoutout());
68 console.log(d4cube.superShout());

 

typescript继承 __extends = (this && this.__extends) || (function () { 代码解读

标签:cal   class   obj   OLE   string   属性   原型   height   设置   

原文地址:https://www.cnblogs.com/montai/p/13357059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!