更新时间:2023-12-05 11:19:16
通常来说,这不需要产生错误。您遇到什么错误?那myObject没有定义?如果是这样,只需添加防护措施并检查其是否已定义。例如:
Generally speaking this shouldn't need to yield an error. What's the error you're getting? That myObject isn't defined? If so, just add a safeguard and check if it's defined. Such as:
if (myObject) {
myObject.width = 315
}
也就是说,您可以通过将其包装在尝试捕获
That said, you can surpress it by wrapping it in a try-catch
try {
myObject.width = 315;
}
catch(err) {
// do nothing
}
要查看正在发生的情况,请尝试运行以下代码,并在删除 var myObject = {}
行时查看正在发生的情况。
To see what's happening, try running the following code and see what's happening when you're removing the var myObject = {}
line.
var myObject = {}
try {
myObject.width = 315;
} catch(err) {
console.log('error');
}
console.log(myObject)