我遇到了未定义变量错误的问题。这是我的代码:
window.sys.Bash = {};
window.sys.Bash.version = "";
window.sys.Bash.version.major = 0;
window.sys.Bash.version.minor = 1;
window.sys.Bash.version.build = 1;
window.sys.Bash.version.release = "beta";
window.sys.Bash.printing = false;
window.sys.Bash.queue = Array();
window.sys.Bash.span = bash;
window.sys.Bash.span.input = input;
window.sys.Bash.version = ""
+ window.sys.Bash.version.major + "."
+ window.sys.Bash.version.minor + "."
+ Array(2-window.sys.Bash.version.build.toString().length+1).join('0')
+ window.sys.Bash.version.build + "-"
+ window.sys.Bash.version.release + " "
+ "(x86_64-" + window.sys.platform + ")";
delete bash; delete input;
我的Web控制台说,window.sys.Bash.version.build在此行未定义:
+ Array(2-window.sys.Bash.version.build.toString().length+1).join('0')
我复制了here中的代码,所以我对此并不了解,但它应该有效,是吗?
答案 0 :(得分:4)
您将version
定义为原始而非对象。试试这个:
window.sys.Bash.version = {};
window.sys.Bash.version.major = 0;
window.sys.Bash.version.minor = 1;
window.sys.Bash.version.build = 1;
向基元添加属性不是错误,但属性将添加到随后丢失的临时对象中。基本上,这发生了:
window.sys.Bash.version = "";
new String(window.sys.Bash.version).major = 0;
new String(window.sys.Bash.version).minor = 1;
new String(window.sys.Bash.version).build = 1;
这描述为here:
设O为ToObject(base)。
实际上与Object(str)
相同,实际上与new String(str)
相同