【问题标题】:JavaScript constructor and properties and object attributes visibilityJavaScript 构造函数和属性以及对象属性的可见性
【发布时间】:2013-11-27 13:31:15
【问题描述】:

我想知道是否可以在对象中本地使用变量 (var) 以使其从外部不可见并同时在其上放置属性(get、set 方法):jsfiddle.net/9jT3B 在示例中的日志中,所有日期都将相同,我认为是因为我在 defineProperties 函数中使用了 Leave 对象的原型,不是吗?

提前谢谢你。

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);

【问题讨论】:

    标签: javascript oop properties


    【解决方案1】:

    我不确定您在这里要做什么,但如果这有助于回答您的问题,请告诉我。

    //Any variables declined outside the function are in the global namespace and accessible   anywhere else
    var globalPublicVariables ='You can See me'
    
    var objectPrototype = function(id, start, end, type, desc, ghost){
        //You don't need to redefine the input values they are already set to this id if they are passed
        //You just need to do the check for ghost to set it to a default value of false
        ghost = ghost||false;
    
        //Any Variables you define inside the function are private to that function
        var _privateVariable = 'You can\'t see me';
    
        //Any functions defined in here and not returned are not public
        var _privateFunction = function(){
            return _privateVariable;
        };
    
        //I like to create a public object that is returned containing any public functions or variables you want accessible outside the prototype
        var publicObject = {};
    
            //Any Variable assigned to this object will be public.
            publicObject.prototypePublicVariable = 'I am publicly accessible and editable';
    
            //Any Function assigned to this object will be publicly accessible
            publicObject.prototypePublicFunction = function(){
                tempReturn = _privateVariable+' but now you can'
                return _privateVariable;
            };
    
        //return the publicObject to make properties of it publicly accessible
        return publicObject;
    };
    
    objectPrototype.prototypePublicVariable; //returns 'I am publicly accessible and editable'
    objectPrototype.prototypePublicFunction; //returns 'You can\'t see me but now you can'
    
    objectPrototype._privateVariable; //This is not accessible outside the function.
    objectPrototype._privateFunction; //This is not accessible outside the function.
    
    //If you want to actually create new instances of a prototype then use this
    var newPrototype = new objectPrototype();
    
    //Now you can use
    newPrototype.prototypePublicVariable; //returns 'I am publicly accessible and editable'
    newPrototype.prototypePublicFunction; //returns 'You can\'t see me but now you can'
    

    【讨论】:

    • 是的,我希望对象变量不能仅通过它们的属性在函数外部访问。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2010-10-07
    • 2011-12-26
    • 2012-07-18
    • 2023-03-29
    相关资源
    最近更新 更多