我不知道为什么我无法访问我的变量。基本上,我假设当appendUnitsIfHasValue函数运行时,它应该可以访问宽度,长度,高度和权重。我也应该能够操纵变量。
注意:console.log打印“我应该有权访问STUFF”;但是,程序一旦到达下一行就会爆炸。它说“未捕获的ReferenceError:宽度未定义”
最终,我希望能够在调用函数时能够访问变量,并且能够更改CreateItem函数中包含的值。提前感谢您的帮助!
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"}
}
答案 0 :(得分:0)
您不应该有权访问这些变量。它们在createItem
函数的范围内定义,只能在该函数中访问。
您是否正在尝试执行类似
的操作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"}
}