0
我想知道是否有可能在對象中使用本地變量(var),以便從外部不可見並將屬性(get,set方法)放在該對象上在同一時間:jsfiddle.net/9jT3B 在日誌中的例子所有日期將是相同的,我認爲這是因爲我在defineProperties函數中使用了Leave對象的原型,不是嗎?JavaScript的構造函數和屬性和對象屬性的可見性
預先感謝您。
var a = [
{
"u": 0,
"l": [
{ "start": new Date(2013, 9, 25), "end": new Date(2013, 10, 2), "type": "B", "desc": "" },
{ "start": new Date(2013, 10, 3), "end": new Date(2013, 10, 3), "type": "O", "desc": "Description 1" },
{ "start": new Date(2013, 10, 9), "end": new Date(2013, 10, 10), "type": "T", "desc": "" },
{ "start": new Date(2013, 10, 23), "end": new Date(2013, 10, 28), "type": "S", "desc": "" },
{ "start": new Date(2013, 10, 29), "end": new Date(2013, 11, 10), "type": "O", "desc": "Description 2" }
]
},
{
"u": 1,
"l": [
{ "start": new Date(2013, 10, 14), "end": new Date(2013, 10, 14), "type": "S", "desc": "" },
{ "start": new Date(2013, 10, 20), "end": new Date(2013, 10, 30), "type": "B", "desc": "" }
]
}
];
function Leave (id, start, end, type, desc, ghost) {
var _id = id;
var _start = start;
var _end = end;
var _type = type;
var _desc = desc;
var _ghost = ghost !== undefined ? ghost : false;
Object.defineProperties(Leave.prototype, {
start: {
set: function (x) {
_start = x;
},
get: function() {
return _start;
},
enumerable: true,
configurable: true
},
end: {
set: function (x) {
_end = x;
},
get: function() {
return _end;
},
enumerable: true,
configurable: true
},
type: {
set: function (x) {
_type = x;
},
get: function() {
return _type;
},
enumerable: true,
configurable: true
},
desc: {
set: function (x) {
_desc = x;
},
get: function() {
return _desc;
},
enumerable: true,
configurable: true
}
});
};
var b = [];
function load(json) {
for(var i = 0; i < json.length; i++) {
var u = [json[i].u, [[], [], [], [], [], [], [], [], [], [], [], []]];
for(var j = 0; j < json[i].l.length; j++) {
var act = new Leave("", json[i].l[j].start, json[i].l[j].end, json[i].l[j].type, json[i].l[j].desc);
u[1][json[i].l[j].start.getMonth()].push(act);
}
b.push(u);
}
}
load(a);
console.log(b);
是的,我想要的對象變量是函數外部訪問只是它們的屬性。謝謝。 – sada