JS删除引用后面的对象而不是删除引用

时间:2015-11-18 12:42:41

标签: javascript

是否可以删除/替换变量引用后面的对象而不是删除/更改本地对象的引用? e.g:

JS小提琴:http://jsfiddle.net/ManuelFink/5neLbocd/

var global_used_objects = {"a":"1","b":"2"};

function TheInstance() {
    var local_used_objects = global_used_objects; 
    this.resetUsedObjects = function() {
        // simulating komplex nesting with same variable name used before;
        var global_used_objects = {}; // global reference lost
		
        // remove all used objects globaly
        local_used_objects = {}; // only resets local scope and removes reference
        global_used_objects = {}; // only resets local scope and removes reference

        // This changes reference of local_used_object from global_used_object to {}. 
        // However I'd like to remove all objects from global_used_objects.
        // in reason of multi-nesting global-used_objects is not accessible here!
    }
    this.getUsedObjects = function() {
        return local_used_objects;
    }
}
var theinstance = new TheInstance();
console.log("local",theinstance.getUsedObjects(), "gobal", global_used_objects);
console.log("reset",theinstance.resetUsedObjects());
console.log("local",theinstance.getUsedObjects(), "gobal", global_used_objects);

我正在使用嵌套singeltons进行复杂的面向对象原型的js应用程序,因此我不能简单地访问父元素!

0 个答案:

没有答案