Null对象中属性默认值的赋值
Assignment of Property Default Values in a Null Object Javascript
我有这个对象TrSet
,它从dom中获取节点。有时这些dom元素可以为null,我想处理节点的属性,不幸的是,我不知道如何在undefined
属性上使用null条件赋值。
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")) {
我不明白为什么这两个都失败了。如果它为空或未定义,它应该对空字符串求值为true,并将其设置为等于该值。
EDIT For Clarity
在第一个例子中
var TrSet = function(txt) {
this.txtValue = txt || "";
this.txtValue.value = txt.value || "";
}
如果txt
为空则设其等于""
,如果txt.value
为空则设其等于""
。这是怎么失败的?
第二个例子
var TrSet = function(stat) {
this.statusDropDown = stat || "";
if (this.statusDropDown.hasOwnProperty("value")) { }
如果stat
为空,则设置statusDropDown
等于""
。然后我检查它是否为hasOwnProperty
。为什么会失败呢?
我不明白为什么这两个都失败了。如果它为空或未定义,它应该对空字符串求值为true,并将其设置为等于该值。
不完全是这样:语句txt || ""
将返回 txt
或""
的值,以先为真者为准。它不会在任何地方赋值,特别是不会赋值给txt
。你单独分配。
因此,在你的例子中(例如,减少到必要的部分):
var TrSet = function(valid,stat,txt,dd){
this.txtValue = txt || "";
console.log('txt will still be undefined but txtValue will not be', txt, this.txtValue);
this.txtValue.value = txt.value || "";
}
要达到预期效果,需要再次默认设置txt
,如下所示:
var TrSet = function(valid,stat,txt,dd){
this.txtValue = txt || "";
console.log('txt will still be undefined but txtValue will not be', txt, this.txtValue);
this.txtValue.value = (txt || "").value || ""; // <- change here
}
当然,这在很大程度上是不可读的(.value || ''
如何组?你一眼就知道吗?),所以我们可能会缓存它:
var TrSet = function(valid,stat,txt,dd){
var realTxt = txt || ""
this.txtValue = realTxt;
this.txtValue.value = realTxt.value || "";
}
现在,这是可行的,但还不够理想。ES6增加了直接在参数列表中提供默认值的功能,这样更简洁:
var TrSet = function(valid = "", stat = "", txt = "", dd = "") {
this.txtValue = txt;
this.txtValue.value = txt.value || "";
}
直接回答你添加的两个简短的问题:
var TrSet = function(txt) { this.txtValue = txt || ""; this.txtValue.value = txt.value || ""; }
如果txt为空,设置它等于",如果txt。值为空,设置它等于"。这是怎么失败的?
因为txt || ""
没有将赋值给txt
,它只是返回 txt
或 ""
,返回值赋值给this.txtValue
。
var TrSet = function(stat) { this.statusDropDown = stat || ""; if (this.statusDropDown.hasOwnProperty("value")) { }
如果stat为null,则设置statusDropDown等于"。然后检查它是否有ownproperty。为什么会失败呢?
可能是因为this
没有绑定,所以它实际上没有statusDropDown
的值。在JavaScript中,this
可以根据函数的调用方式而改变。
这一行:
this.txtValue.value = txt.value || "";
txt
是null
,尝试在null对象上引用属性会导致错误。你需要把它改成:
this.txtValue.value = txt ? (txt.value || "") : "";
在解引用变量之前测试它是否为空。
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- jquerydatetimepicker日期和时间应在框默认值中,而无需单击框
- 如果值为空,如何设置输入的默认值?jQuery
- 骨干模型默认值-todos.js示例中不必要的代码
- jquery日期选择器年份范围默认值
- Html5输入属性的默认值,如最小值、最大值、大小等
- 根据两个下拉列表的默认值禁用按钮
- 如何创建具有默认值的JavaScript对象字段?(AngularJS模型相关)
- Angularjs-设置不带ng选项的select的默认值
- jQuery Datepicker从日期开始设置默认值
- 如何设置浏览器打开的文件对话框的文件名(一些默认值)
- JQuery BlockUI鼠标加载光标没有't在Google Chrome中返回默认值
- Selectize选择默认值
- 在KeystoneJS中为Types.Money设置默认值
- 是否可以同时为一个元素的所有事件指定阻止默认值
- 防止在IE7中按回车键时出现默认值
- 如何覆盖jqGrid's语言默认值
- 如何为显示jquery滑块值的文本框设置默认值
- ES6构造对象,在赋值时不用作函数参数时的默认值
- Null对象中属性默认值的赋值