0
我不知道爲什麼我不再有權訪問我的變量。基本上,我假設當appendUnitsIfHasValue函數運行時,它應該可以訪問寬度,長度,高度和權重。我應該也可以操縱變量。Backbone.js的範圍界定問題
注意:console.log打印「我應該訪問到東西」;但是,程序一旦到達下一行就會爆炸。它說「未捕獲的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"}
}
我認爲,由於功能的作用域,我仍然有機會獲得類似於以下變量中:var x = 5; increment = function(){x = x + 1};增量() – user3292094