Microsoft JScript throws 'variable' is undefined error when setting variable to undefined
I'm setting a variable i开发者_JAVA技巧n Microsoft JScript to another variable that is undefined, i.e.,
var valid = Page_IsValid;
JScript throws the following error
Microsoft JScript runtime error: 'Page_IsValid' is undefined
Somehow this doesn't strike me as correct. Shouldn't it just ignore the assignment -- I don't do anything with Page_IsValid beyond assigning it to the variable.
That's the expected behaviour, your attempting to assign something that does not exist.
You can test the state of Page_IsValid and handle it as neccesary:
var valid = (typeof Page_IsValid !== 'undefined') ? Page_IsValid : <default value>;
This is correct behavior.  You can reference a new variable without the var keyword so long as your first reference is an assignment (e.g., firstReference = 'foo';), or in a typeof expression.  This is because you are still declaring the variable, but as a global.  In your case, you are trying to use Page_IsValid on the right-hand of the assignment, but the interpreter has no idea what to do with it, because it hasn't been delcared anywhere.
If you're unsure whether or not Page_IsValid will have been declared or not, you can do something like this:
// kind of funky to set the value to undefined, but this ensures
// that Page_IsValid has been properly "declared"
if (typeof Page_IsValid === 'undefined') { Page_IsValid = undefined; }
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论