From 434a5858efff9e6b6d4cd10d35eaffe80a9e5583 Mon Sep 17 00:00:00 2001 From: vmwxiong <71673910+vmwxiong@users.noreply.github.com> Date: Thu, 17 Jun 2021 22:30:04 -0700 Subject: [PATCH] Add support for class getters and setters --- lib/create-script-decorator.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/create-script-decorator.ts b/lib/create-script-decorator.ts index ff8b475..3f5012e 100644 --- a/lib/create-script-decorator.ts +++ b/lib/create-script-decorator.ts @@ -24,6 +24,28 @@ export const createScript = function(app?: pc.Application) { script.prototype[prop] = instance[prop]; } } + + // Add getters and setters to prototype + const prototypePropertyDescriptors = Object.getOwnPropertyDescriptors( + obj.prototype + ); + for (const prop in prototypePropertyDescriptors) { + if (prop !== "constructor") { + const descriptors = prototypePropertyDescriptors[prop]; + const { get, set } = descriptors; + + if (get || set) { + const scriptDescriptor = { + get, + set, + enumerable: false, + configurable: true, + }; + + Object.defineProperty(script.prototype, prop, scriptDescriptor); + } + } + } // Add static properties for (let prop in obj) { @@ -60,4 +82,4 @@ export class ScriptTypeBase { * @memberof ScriptType */ enabled: boolean; -} \ No newline at end of file +}