{"id":134845,"date":"2020-07-07T21:57:00","date_gmt":"2020-07-07T13:57:00","guid":{"rendered":"http:\/\/4563.org\/?p=134845"},"modified":"2020-07-07T21:57:00","modified_gmt":"2020-07-07T13:57:00","slug":"typescript-%e7%94%a8%e8%a3%85%e9%a5%b0%e5%99%a8%e9%81%87%e5%88%b0-this-%e6%8e%a8%e5%af%bc%e4%b8%8d%e5%af%b9%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=134845","title":{"rendered":"typescript \u7528\u88c5\u9970\u5668\u9047\u5230 this \u63a8\u5bfc\u4e0d\u5bf9\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  typescript \u7528\u88c5\u9970\u5668\u9047\u5230 this \u63a8\u5bfc\u4e0d\u5bf9\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u662f\u4e3a\u7c7b\u7684\u65b9\u6cd5\u5199\u7684\u88c5\u9970\u5668\u51fd\u6570<\/p>\n<pre><code>function autoSave (_target: BookmarkModel, _name: string, descriptor: PropertyDescriptor): PropertyDescriptor {   const value = descriptor.value   descriptor.value = function (...args: never): boolean {     const ret = value.apply(this, args)     if (ret === true) {       this.save()     }     return ret   }   return descriptor } <\/code><\/pre>\n<p>\u4ee3\u7801\u5f3a\u884c\u6267\u884c\u662f\u5b8c\u5168 OK \u7684\uff0c\u4f46\u662f this.save()\u8fd9\u884c\u62a5\u4e2a\u9519\uff1a<\/p>\n<p>\u7c7b\u578b\u201cPropertyDescriptor\u201d\u4e0a\u4e0d\u5b58\u5728\u5c5e\u6027\u201csave\u201d\u3002<\/p>\n<p>this.save()\u5b9e\u9645\u5c31\u662f\u6267\u884c\u7c7b\u5b9e\u4f8b\u4e0a\u7684 save \u65b9\u6cd5\u5b8c\u5168\u6ca1\u95ee\u9898\uff0c\u4f46\u73b0\u5728\u8fd9\u6837\u6211\u5c31\u5f88\u61f5\uff0c\u600e\u4e48\u89e3\u51b3<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>18<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2432881\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhyl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u53c2\u6570\u91cc\u6307\u5b9a this \u7c7b\u578b\u3002<br \/>2. this \u8f6c\u6362\u6210 any \u518d\u8c03\u7528\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432882\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u521d\u5b66 TS \u778e\u8bd5\u4e00\u756a\uff0c(this as MyClass).save()\uff0c\u8fd9\u6837\u5c31\u89e3\u51b3\u4e86\uff0c\u4e0d\u8fc7\u770b\u8d77\u6765\u4f3c\u4e4e\u4e0d\u592a\u4f18\u96c5\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432883\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oott123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u4f60\u6e05\u695a\u81ea\u5df1\u7684 this \u662f\u4ec0\u4e48\u7c7b\u578b\uff1a<\/p>\n<p>descriptor.value = function (this: YourTypeHere, &#8230;args: never): boolean {<\/p>\n<p>\u5982\u679c this \u548c autoSave \u7684\u4e0a\u4e0b\u6587\u76f8\u540c\uff1a<\/p>\n<p>descriptor.value = (&#8230;args: never): boolean =&gt; {                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432884\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : optional <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u91cc\u4f60\u7528 this \u5e72\u561b\uff0c\u7528_target \u554a\uff0c_target \u4e0d\u5c31\u662f\u4f60\u7684 this \u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432885\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rabbbit <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             interface PropertyDescriptor {<br \/>\u3000 save?: Function;<br \/>}<\/p>\n<p>function autoSave(_target: BookmarkModel, _name: string, descriptor: PropertyDescriptor): PropertyDescriptor {<br \/>\u3000 const value = descriptor.value<br \/>\u3000 descriptor.value = function (&#8230;args: never): boolean {<br \/>\u3000\u3000 const ret = value.apply(this, args)<br \/>\u3000\u3000 if (this.save) { \/\/ &lt;&#8211;<br \/>\u3000\u3000\u3000 this.save();<br \/>\u3000\u3000}<br \/>\u3000\u3000 return ret<br \/>\u3000}<br \/>\u3000 return descriptor<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432886\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : vuevue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u51fd\u6570\u8c03\u7528\u65f6\u7528 bind(this)\u8bd5\u8bd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432887\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @oott123 #3 `descriptor.value = function (this: YourTypeHere, &#8230;args: never)`\u8fd9\u4e48\u641e\u76f8\u5f53\u4e8e\u6539\u53d8\u539f\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\uff0c\u8c03\u7528\u65b9\u6cd5\u7684\u65f6\u5019\u8fd8\u5f97\u989d\u5916\u4f20\u4e2a this \u53c2\u6570\uff0c\u4e0d\u884c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432888\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @optional #4 https:\/\/es6.ruanyifeng.com\/#docs\/decorator<\/p>\n<p>\u88c5\u9970\u5668\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7c7b\u7684\u539f\u578b\u5bf9\u8c61\uff0c\u4e0a\u4f8b\u662f Person.prototype\uff0c\u88c5\u9970\u5668\u7684\u672c\u610f\u662f\u8981\u201c\u88c5\u9970\u201d\u7c7b\u7684\u5b9e\u4f8b\uff0c\u4f46\u662f\u8fd9\u4e2a\u65f6\u5019\u5b9e\u4f8b\u8fd8\u6ca1\u751f\u6210\uff0c\u6240\u4ee5\u53ea\u80fd\u53bb\u88c5\u9970\u539f\u578b\uff08\u8fd9\u4e0d\u540c\u4e8e\u7c7b\u7684\u88c5\u9970\uff0c\u90a3\u79cd\u60c5\u51b5\u65f6 target \u53c2\u6570\u6307\u7684\u662f\u7c7b\u672c\u8eab\uff09\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6240\u8981\u88c5\u9970\u7684\u5c5e\u6027\u540d\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8be5\u5c5e\u6027\u7684\u63cf\u8ff0\u5bf9\u8c61\u3002<\/p>\n<p>\u5b9e\u6d4b\u7ed3\u679c\u786e\u5b9e\u5982\u6b64_target \u4e0d\u662f this \u800c\u662f\u7c7b\u672c\u8eab                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432889\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rabbbit #5 \u672c\u6765 save \u5c31\u4e0d\u662f PropertyDescriptor \u91cc\u9762\u7684\u65b9\u6cd5\uff0c\u5f3a\u884c\u7ed9\u4ed6\u52a0\u4e2a\u5b9a\u4e49\uff0c\u5e76\u4e14\u8fd9\u6837\u8fd8\u8986\u76d6\u6389\u539f\u7248 PropertyDescriptor\uff0c\u4e0d\u884c\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432890\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9 typescript \u7684\u7c7b\u578b\u63a8\u5bfc\u7279\u522b\u50bb\u903c\uff0c\u4e00\u70b9\u90fd\u4e0d\u667a\u80fd\u3002\u6bd4\u5982\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u6570\u7ec4\u5c31\u7ed9\u63a8\u5bfc\u6210[never]\uff0c\u5b8c\u5168\u4e0d\u5206\u6790\u540e\u7eed\u4f7f\u7528\u7684\u4e0a\u4e0b\u6587\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432891\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : optional <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rikka \u5982\u679c\u4f60\u7684 save \u662f\u76f4\u63a5\u7531 BookmarkModel \u5b9a\u4e49\u7684\uff0c\u90a3\u4e48\u5b83\u7684 prototype \u4e0a\u5c31\u6709 save \u65b9\u6cd5\u554a\u3002<\/p>\n<p>_target.save.call(this,args)\u4e0d\u53ef\u4ee5\u5417                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432892\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oott123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rikka #7 \u4f60\u786e\u5b9a\u4f60\u8bd5\u8fc7\u4e86\u5417<\/p>\n<p>https:\/\/www.typescriptlang.org\/play\/index.html#code\/GYVwdgxgLglg9mABAQwBRQBYwM4C5HZQBOMYA5gDQpFkCM+YIAtgEYCmRAlIgN4BQfAL5A<\/p>\n<p>\u770b\u53f3\u8fb9\u7684\u7f16\u8bd1\u7ed3\u679c\uff0cthis \u4f1a\u88ab\u7f16\u8bd1\u6389\uff0c\u53ea\u505a\u7c7b\u578b\u63d0\u793a\u4f7f\u7528\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432893\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @optional #11 \u4f60\u8fd9\u6837\u786e\u5b9e\u4e5f\u53ef\u4ee5\uff0c\u4e0d\u8fc7\u5e94\u8be5\u662f_target.save.apply(this,args)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432894\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @oott123 #12 \u539f\u6765\u53ef\u4ee5\u662f\u8fd9\u6837\u554a\uff0c\u6ca1\u8bd5\u8fc7\uff0c\u5b66\u4e60\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432895\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rabbbit <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u4e00\u5b9a\u975e\u8981\u8986\u76d6 PropertyDescriptor , \u4f60\u53ef\u4ee5\u7528 extends \u6269\u5c55\u5b83.<br \/>\u5982\u679c \u4e00\u5f00\u59cb\u4e0d\u5b58\u5728 save \u65b9\u6cd5, \u4e4b\u540e\u53ef\u80fd\u4f1a\u6dfb\u52a0. \u5219\u8981\u5728\u521b\u5efa\u65f6\u4f7f\u7528 ?: \u8868\u793a\u53ef\u80fd\u5b58\u5728\u8fd9\u4e2a\u5c5e\u6027.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432896\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : rikka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rabbbit #15 save \u65b9\u6cd5\u65e2\u4e0d\u662f\uff0c\u4e5f\u4e0d\u5e94\u8be5\u662f PropertyDescriptor \u7684\u6210\u5458\uff0c\u5373\u4fbf\u4e0d\u8986\u76d6\u6269\u5c55\u5b83\u4e5f\u662f\u975e\u5e38\u4e0d\u5408\u7406\u7684\u505a\u6cd5\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432897\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Justin13 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ts-ignore                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2432898\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zmlq7 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7b2c\u4e09\u4e2a\u662f\u5b98\u7f51\u4ecb\u7ecd\u4e0a\u7684\u65b9\u6cd5\uff0c\u7f16\u8bd1\u4f1a\u53bb\u6389 this \u8fd9\u4e2a\u53c2\u6570\u3002\u5b98\u7f51\u63a8\u8350\u5c31\u633a\u597d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>typescript \u7528\u88c5\u9970\u5668\u9047\u5230&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/134845"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134845"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/134845\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134845"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}