Frage

I have no idea as to why I no longer have access to my variables. Basically, I am under the assumption that when the appendUnitsIfHasValue function runs, it should have access to width, length, height, and weight. I should be able to manipulate the variables as well.

Note: the console.log prints "I SHOULD HAVE ACCESS TO STUFF"; however, the program explodes once it reaches the next line. It says that " Uncaught ReferenceError: width is not defined "

Ultimately, I want to be able to have access to the variables when I call the function and be able to change the values contained within the CreateItem function. Thank you in advance for the help!

CatalogApp.prototype.createItem = function(event) {
  event.preventDefault()
  submittedForm = this
  var name = submittedForm["name"].value
  var description = submittedForm["description"].value
  var width = submittedForm["width"].value
  var length = submittedForm["length"].value
  var height = submittedForm["height"].value
  var weight = submittedForm["weight"].value
  that.appendUnitsIfHasValue(width, length, height, weight)
}

CatalogApp.prototype.appendUnitsIfHasValue = function(a,b,c,d) {
  console.log("I SHOULD HAVE ACCESS TO STUFF")
  if (width != "") {width+=' '+DIMENSIONSUNIT} else {width = "N/A"}
  if (length != "") {length+=' '+DIMENSIONSUNIT} else {length = "N/A"}
  if (height != "") {height+=' '+DIMENSIONSUNIT} else {height = "N/A"}
  if (weight != "") {weight+=' '+WEIGHTUNIT} else {weight = "N/A"}
}
War es hilfreich?

Lösung

You should not have access to those variables. They're defined in the scope of the createItem function and are only accessible within that function.

Are you trying to do something like

CatalogApp.prototype.appendUnitsIfHasValue = function(a,b,c,d) {
  if (a != "") {a+=' '+DIMENSIONSUNIT} else {a = "N/A"}
  if (b != "") {b+=' '+DIMENSIONSUNIT} else {b = "N/A"}
  if (c != "") {c+=' '+DIMENSIONSUNIT} else {c = "N/A"}
  if (d != "") {d+=' '+WEIGHTUNIT} else {d = "N/A"}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top