Question

I'm having a problem with an undefined variable error. This is my code:

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;

My Web console says, that window.sys.Bash.version.build is undefined on this line:

    + Array(2-window.sys.Bash.version.build.toString().length+1).join('0')

I copied the code from here, so I don't know much about it, but it should work, huh?

Was it helpful?

Solution

You defined version as primitive, rather than object. Try this:

window.sys.Bash.version = {};
window.sys.Bash.version.major = 0;
window.sys.Bash.version.minor = 1;
window.sys.Bash.version.build = 1;

Adding properties to primitive is not an error, but the properties will be added to a temporary object that is then lost. Basically, this happened:

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;

This is described here:

Let O be ToObject(base).

Which is effectively the same as Object(str), which is effectively the same as new String(str)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top