创建了一个方法,这样的写法,在ArkTS中,是否为最优解?是否还有其他实现方式?对内存消耗最低的方式是什么?
export class TextUtil {
public static isEmpty(str: string | null | undefined): boolean {
return str === null || str === undefined || str.length === 0 || str === ""
}
}
下列写法如何在ArkTS中实现?
const Text = {
isEmpty (str: string | null | undefined){
return str === null || str === undefined || str.length === 0 || str === ""
}
}
String.prototype.isEmpty = function (){
return this === null || this === undefined || this.length === 0 || this === ""
}
class的实例化和销毁都会涉及到内存分配和释放,这些操作都会有一定的性能开销。function的调用和返回也会涉及到内存分配和释放,但由于函数调用的临时性,这些操作的性能开销通常较小。提供代码的写法无法实现,不支持扩展原型。