diff --git a/src/sandbox/__tests__/proxySandbox.test.ts b/src/sandbox/__tests__/proxySandbox.test.ts index be6ae0b..815754b 100644 --- a/src/sandbox/__tests__/proxySandbox.test.ts +++ b/src/sandbox/__tests__/proxySandbox.test.ts @@ -287,6 +287,15 @@ test('bounded function should not be rebounded', () => { expect(isBoundedFunction(proxy.fn1)).toBeTruthy(); }); +test('the prototype should be kept while we create a function with prototype on proxy', () => { + const proxy = new ProxySandbox('new-function').proxy as any; + + function test() {} + proxy.fn = test; + expect(proxy.fn === test).toBeFalsy(); + expect(proxy.fn.prototype).toBe(test.prototype); +}); + test('some native window property was defined with getter in safari and firefox, and they will check the caller source', () => { Object.defineProperty(window, 'mockSafariGetterProperty', { get(this: Window) { diff --git a/src/sandbox/common.ts b/src/sandbox/common.ts index 0e21915..80e77ff 100644 --- a/src/sandbox/common.ts +++ b/src/sandbox/common.ts @@ -35,8 +35,11 @@ export function getTargetValue(target: any, value: any): any { boundValue[key] = value[key]; } - // copy prototype, for performance reason, we use in operator to check rather than hasOwnProperty - if ('prototype' in value) boundValue.prototype = value.prototype; + // copy prototype if bound function not have + // mostly a bound function have no own prototype, but it not absolute in some old version browser, see https://github.com/umijs/qiankun/issues/1121 + if (value.hasOwnProperty('prototype') && !boundValue.hasOwnProperty('prototype')) + boundValue.prototype = value.prototype; + functionBoundedValueMap.set(value, boundValue); return boundValue; }