From ebfa90448463178a9101b7579ca7cc2c7559e813 Mon Sep 17 00:00:00 2001 From: saber2pr <1029985799@qq.com> Date: Fri, 5 Feb 2021 16:55:11 +0800 Subject: [PATCH 1/2] fix: clone object extends null --- src/core/util.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/core/util.ts b/src/core/util.ts index 2ea269107..c87c2ea9d 100644 --- a/src/core/util.ts +++ b/src/core/util.ts @@ -29,6 +29,7 @@ const TYPED_ARRAY: {[key: string]: boolean} = { }; const objToString = Object.prototype.toString; +const objHasOwnProperty = Object.prototype.hasOwnProperty const arrayProto = Array.prototype; const nativeForEach = arrayProto.forEach; @@ -106,7 +107,7 @@ export function clone(source: T): T { else if (!BUILTIN_OBJECT[typeStr] && !isPrimitive(source) && !isDom(source)) { result = {} as any; for (let key in source) { - if (source.hasOwnProperty(key)) { + if (hasOwn(source as object, key)) { result[key] = clone(source[key]); } } @@ -131,7 +132,7 @@ export function merge(target: any, source: any, overwrite?: boolean): any { } for (let key in source) { - if (source.hasOwnProperty(key)) { + if (hasOwn(source, key)) { const targetProp = target[key]; const sourceProp = source[key]; @@ -184,7 +185,7 @@ export function extend< } else { for (let key in source) { - if (source.hasOwnProperty(key)) { + if (hasOwn(source, key)) { (target as S & T)[key] = (source as T & S)[key]; } } @@ -244,7 +245,7 @@ export function inherits(clazz: Function, baseClazz: Function) { clazz.prototype = new (F as any)(); for (let prop in clazzPrototype) { - if (clazzPrototype.hasOwnProperty(prop)) { + if (hasOwn(clazzPrototype, prop)) { clazz.prototype[prop] = clazzPrototype[prop]; } } @@ -318,7 +319,7 @@ export function each | any[] | readonly any[] | ArrayL } else { for (let key in arr) { - if (arr.hasOwnProperty(key)) { + if (hasOwn(arr, key)) { cb.call(context, (arr as Dictionary)[key], key as any, arr); } } @@ -439,7 +440,7 @@ export function keys(obj: T): (KeyOfDistributive & string)[ } let keyList: TKeys[] = []; for (let key in obj) { - if (obj.hasOwnProperty(key)) { + if (hasOwn(obj, key)) { keyList.push(key as any); } } @@ -682,7 +683,7 @@ export class HashMap { // (We usually treat `null` and `undefined` as the same, different // from ES6 Map). get(key: KEY): T { - return this.data.hasOwnProperty(key) ? this.data[key] : null; + return hasOwn(this.data, key) ? this.data[key] : null; } set(key: KEY, value: T) { // Comparing with invocation chaining, `return value` is more commonly @@ -696,7 +697,7 @@ export class HashMap { context?: Context ) { for (let key in this.data) { - if (this.data.hasOwnProperty(key)) { + if (hasOwn(this.data, key)) { cb.call(context, this.data[key], key); } } @@ -767,8 +768,8 @@ export function createObject(proto?: object, properties?: T): T { return obj; } -export function hasOwn(own: object, prop: string): boolean { - return own.hasOwnProperty(prop); +export function hasOwn(own: object, prop: PropertyKey): boolean { + return objHasOwnProperty.call(own, prop) } export function noop() {} From ffb63bd5d86b2ea89c52d8b6cc8a2d6723470add Mon Sep 17 00:00:00 2001 From: Konpaku Youmu Date: Fri, 5 Feb 2021 19:42:03 +0800 Subject: [PATCH 2/2] fix: clone object extends null check the hasOwnProperty function exists --- src/core/util.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/util.ts b/src/core/util.ts index c87c2ea9d..e3bf3edd5 100644 --- a/src/core/util.ts +++ b/src/core/util.ts @@ -769,6 +769,9 @@ export function createObject(proto?: object, properties?: T): T { } export function hasOwn(own: object, prop: PropertyKey): boolean { + if (own.hasOwnProperty) { + return own.hasOwnProperty(prop) + } return objHasOwnProperty.call(own, prop) }