日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Typescript中class的extends码源分析

發(fā)布時(shí)間:2024/9/5 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Typescript中class的extends码源分析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

學(xué)習(xí)typescript的樂(lè)趣在于看它的碼源是如何要js實(shí)現(xiàn)的、

今天要分析的是類繼承的碼源。我們先來(lái)看一下使用ES5的組合繼承是如何做到的:

1 function Person(name) { 2 this.name = name; 3 } 4 Person.prototype.sayName = function () { 5 console.log(this.name); 6 }; 7 function Student(name, school) { 8 Person.call(this, name) 9 this.school = school; 10 } 11 Student.prototype = Person.prototype; 12 Student.prototype.saySchool = function () { 13 console.log(this.school); 14 }; 15 let student = new Student('wangting', 'NT'); 16 console.log('school' in student); 17 console.log('saySchool' in student); 18 console.log('name' in student); 19 console.log('sayName' in student);

再來(lái)看一下typescript的實(shí)現(xiàn):

1 var __extends = (this && this.__extends) || (function () { 2 var extendStatics = 3 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 function (d, b) { 7 // 類屬性和類方法的繼承 8 extendStatics(d, b); 9 10 function __() { this.constructor = d; } 11 // d的原型對(duì)象 12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 13 }; 14 })(); 15 var Person = (function () { 16 function Person(name) { 17 this.name = name; 18 } 19 Person.prototype.sayName = function () { 20 console.log(this.name); 21 }; 22 Person.Country = 'China'; 23 return Person; 24 }()); 25 var Student = (function (_super) { 26 __extends(Student, _super); 27 function Student(name, school) { 28 var _this = _super.call(this, name) || this; 29 _this.school = school; 30 return _this; 31 } 32 Student.prototype.saySchool = function () { 33 console.log(this.school); 34 }; 35 return Student; 36 }(Person)); 37 var student = new Student('wangting', 'NT'); 38 console.log(Student.Country); // China 39 student.saySchool(); // NT 40 student.sayName(); // wangting

?

轉(zhuǎn)載于:https://www.cnblogs.com/wangtingnoblog/p/10390699.html

總結(jié)

以上是生活随笔為你收集整理的Typescript中class的extends码源分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。