Skip to content

Commit 717e455

Browse files
committed
fix: fix issue of custom type
1 parent 0e71d93 commit 717e455

File tree

1 file changed

+26
-1
lines changed
  • packages/vrender-animate/src/executor

1 file changed

+26
-1
lines changed
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import { isFunction } from '@visactor/vutils';
22

3+
/**
4+
* 获取自定义类型
5+
* @param custom 自定义动画对象
6+
* @returns 0: 不是函数/类, 1: 是类, 2: 是函数
7+
*/
38
export function getCustomType(custom: any): number {
4-
return custom && isFunction(custom) ? (/^class\s/.test(Function.prototype.toString.call(custom)) ? 1 : 2) : 0;
9+
if (!custom || !isFunction(custom)) {
10+
return 0;
11+
}
12+
// 正则表达式检查是最快的,优先使用
13+
const functionStr = Function.prototype.toString.call(custom);
14+
if (/^class\s/.test(functionStr)) {
15+
return 1;
16+
}
17+
// 检查箭头函数(没有prototype)
18+
if (!custom.prototype) {
19+
return 2;
20+
}
21+
// 检查构造函数是否是它自己(ES5类)
22+
if (custom.prototype.constructor === custom) {
23+
// 检查prototype是否可写,类的prototype是不可写的
24+
const descriptor = Object.getOwnPropertyDescriptor(custom, 'prototype');
25+
if (descriptor && !descriptor.writable) {
26+
return 1;
27+
}
28+
}
29+
return 2;
530
}

0 commit comments

Comments
 (0)