I'm getting the error 'foo' is undefined.
in my script when i test my function with an undefi开发者_运维问答ned parameter. As far as I understand, This shouldn't be happening.
My calling code:
//var foo
var test = peachUI().stringIsNullOrEmpty(foo) ;
My function (part of a larger framework).
stringIsNullOrEmpty: function (testString) {
/// <summary>
/// Checks to see if a given string is null or empty.
/// </summary>
/// <param name="testString" type="String">
/// The string check against.
/// </param>
/// <returns type="Boolean" />
var $empty = true;
if (typeof testString !== "undefined") {
if (testString && typeof testString === "string") {
if (testString.length > 0) {
$empty = false;
}
}
}
return $empty;
}
Any ideas?
Please note. I've had a good read of other similar questions before posting this one.
You can't pass a variable that doesn't exist (undefined
, not null
...which does exist) into a function, it's trying to get the value of foo
to pass it when you call
var test = peachUI().stringIsNullOrEmpty(foo);
...and it isn't there, so you're getting the error on just that line, just as you would with a simpler case:
alert(foo);
Now if you tried to call it as a property of something, then it'd be valid, for example:
alert(window.foo);
Then undefined
gets passed in, because it's an undefined property on a known/present object.
I get the error too, but it is is not related to your code, it is because you pass a variable that does not exist to a function. This works:
var foo = undefined;
var test = peachUI().stringIsNullOrEmpty(foo) ;
Btw. the error already tells you that the problem is not in your function, otherwise it be 'testString' is undefined
.
精彩评论