我有一个迷你框架的以下代码。我如何“防弹”我的代码,以便可能的拖欠开发人员不会破坏它?我在评论中总结了代码的作用,这里有一个演示以供澄清
var kit = (function() {
'use strict';
//internal cache
var internal = {
core: {}
}
//external interface for extensions
//let's say we provide a hook for error handling from the core
var external = {
core: {
error: {
addListener: function(callback) {
callback();
}
}
}
}
//externally available options
var core = {
//build extension by creating an instance of the passed function
//providing it the core interface, and
//then store the instance in the cache
extend: function(extensionName, extensionDefinition) {
var newExtension = new extensionDefinition(external.core)
internal.core[extensionName] = newExtension;
}
};
//expose
return {
core: {
extend: core.extend
},
_internal: internal,
_external: external
}
}());
//let's say two developers built these extensions
//developer1 builds his code
kit.core.extend('extension1', function(core) {
core.error.addListener(function() {
alert('test');
})
//developer1 intentionally kills the whole error interface
core.error = null;
});
//now, the other scripts cannot use the error interface
//because some delinquent developer killed it
kit.core.extend('extension2', function(core) {
//core.error is no more!
core.error.addListener(function() {
alert('test');
})
});
我该如何做到这一点,以便每个扩展都有一个独立的core
外部函数“副本”,所以无论它们对它做什么,它都不会影响其他扩展?
一个附带的问题,如果我可以补充的话:有没有更好的方法/方法来构建这个代码?