mootools实现具有隐藏和受保护属性的Node类

时间:2012-06-17 21:59:45

标签: javascript mootools

我想实现如下所示的Node类型,但我想隐藏或保护不是函数的属性,因此不能从类外部重写它们。这可能与Mootools隐藏和保护属性?在Mootools中隐藏和保护有什么区别?

var Node = new Class({

    initialize : function(name){
        this.globalId = container.globalId++;
        this.name = name || 'unnamed Node';
    },

    globalId : null,

    siblingId : null,

    name : null,

    parentNode: null,

    childrenNodes : [],

    addChild : function(child){
        //ensure it is a Node object
        if(!instanceOf(child,Node)){
            throw new Exception('Not a Node',this.name+':\nNode.globalId = '+this.globalId+
            "\nAttempting to add a child node that is not a 'Node' type!");
        }
        child.parentNode = this;
        child.siblingId = this.children.length;
        this.children.push(child);
    },

    removeChild : function(child){
        //ensure the child is my child!
        if(child.parentNode !== this){
            throw new Exception('Lost Child',this.name+':\nNode.globalId = '+this.globalId+
            "\nAttempting to remove Node:\n"+child.name+":\nNode.globalId = "+child.globalId);
        }
        this.childrenNodes.splice(child.siblingId,1);
    }

});
Node.globalId = 0;

1 个答案:

答案 0 :(得分:1)

如果你需要隐藏的属性,你可以这样得到它们:

var Node = (function() { 
    var someProperty; // This variable and any others you create here are available only inside this function's scope

    return new Class({
        // You have access to those variables here
    });
}()); // call the function immediately. It returns your new class.