gpt4 book ai didi

用于检查变量是否存在以避免错误的 JavaScript 函数

转载 作者:行者123 更新时间:2023-11-30 16:14:57 25 4
gpt4 key购买 nike

<分区>

我如何在 JavaScript 中创建实用程序辅助函数来检查变量是否存在以避免错误 Uncaught ReferenceError: testVar is not defined

下面是我尝试做但失败的事情!

/**
* Utility Helper Functions
*/
var Utility = {

/**
* Check if a variable is empty or not
* @param mixed value - variable to check if it is empty and exist
* @return Boolean - true/false if a variable is empty or not?
*/
isEmpty: function(value){
//return (value == null || value === '');
return (typeof value === 'undefined' || value === null);
},
}

// comment var test out so it will not exist for test
//var testVar = 'dgfd';

// Uncaught ReferenceError: testVar is not defined
alert(Utility.isEmpty(testVar));

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com