I have this object TrSet that takes in nodes from the dom. Sometimes those dom elements can be null, and I want to handle properties of the nodes, unfortunately, I can't figure out how to use null conditional assignment on undefined properties.
var TrSet = function(valid,stat,txt,dd){
this.validateField = valid || "";
this.statusDropDown = stat || "";
this.txtValue = txt || "";
this.ddValue = dd || "";
this.txtValue.value = txt.value || ""; //Cannot read property value of undefined
this.ddValue.style.visibility = dd.style.visibility || "hidden"; //same
this.validate = function () {
/// Even when attempting to access
/// the property with hasOwnProperty throws the error
///"Cannot read property 'hasOwnProperty' of null"!
if (this.statusDropDown.hasOwnProperty("value") && this.validateField.hasOwnProperty("style")) {
I don't understand how either of these is failing. If it's null or undefined, it should evaluate to true for the empty string and set it equal to that.
EDIT For Clarity
In the first example
var TrSet = function(txt) {
this.txtValue = txt || "";
this.txtValue.value = txt.value || "";
}
If txt is null set it equal to "", if txt.value is null set it equal to "". how does this fail?
Second example
var TrSet = function(stat) {
this.statusDropDown = stat || "";
if (this.statusDropDown.hasOwnProperty("value")) { }
If stat is null set statusDropDown equal to "". Then I check if if it hasOwnProperty. Why does this fail?
this.statusDropDown.hasOwnProperty("value")thethisrefers to the function currently being executed - i.e., the one assigned tothis.validate- you won't get the properties from outside.statusDropDown, you have an entirely newthiswhich does not have astatusDropDownproperty.