我正在声明一个带有私有字段的对象,其中一些私有对象字段依赖于兄弟对象值的值。我该如何访问它们?
示例:
我想访问:private.fields.url
来自:private.options.static
我需要在这里减少重复:
var Hi = (function (parent, $) {
var public = parent.Page = parent.Page || {};
var private = {
fields: {
url: '#page_url'
},
options: {
static: [
'#page_url'
]
}
};
var form = function () {
var form = $('form[name=page]');
var type = form.find('#page_type');
// hide all fields
initialHideFields(type);
};
var initialHideFields = function (type) {
var selected = type.find('option:selected');
if (selected.length) {
console.log(selected.text());
}
else {
$.each(private.fields, function (key, value) {
$(value).parent().hide();
});
}
};
public.init = function () {
form();
};
return parent;
}(Hi || {}, window.jQuery));
$(function () {
Hi.Page.init();
});
答案 0 :(得分:1)
您无法引用/访问同一对象内的对象属性。相反,你可以这样做:
var private = {
fields: {
url: '#page_url'
},
options: {
static: []
}
};
private.options.static.push(private.fields.url);
console.log(private.options.static);