0

我想实现如下所示的 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;
4

1 回答 1

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.
于 2012-06-18T03:56:34.073 回答