简体   繁体   English

通过AJAX删除新添加的功能

[英]Delete newly added functions via AJAX

I have this original function : 我有这个原始功能:

<script>
function f(x){..}
function z(x){..}
...
</script>

These new functions are loaded later via AJAX : 这些新功能稍后会通过AJAX加载:

<script>
function xyz(){..}
windows.abc(){..}
...
</script>

How do I delete ALL functions binded in the "this" and "windows" scope except the original ones ( in this case f(x), z(x) ). 如何删除绑定在“ this”和“ windows”范围内的所有函数(原始函数除外)(本例中为f(x),z(x))。 I need this to be dynamic meaning I have no idea what the new functions will be except that they will be binded to the "this" and "windows" scope. 我需要这是动态的,除了将新功能绑定到“ this”和“ windows”作用域之外,我不知道新功能是什么。 I'm thinking storing the original function names and not delete those during a loop. 我正在考虑存储原始函数名称,而不是在循环中删除它们。

I'm not sure what the context is, but avoid polluting the global scope with different values. 我不确定上下文是什么,但要避免使用不同的值污染全局范围。

Store an array of your current global functions: 存储您当前的全局函数数组:

// Collect existing functions into an object
// It will look like this: { f: 1, z: 1 }
var funcs = {};
Object.keys(window).filter(function (c) {
   typeof window[c] === "function" && (funcs[c] = 1);
});

// Load new stuff
$.ajax(..., function () {
   // After loading it, delete the functions that didn't exist before
   Object.keys(window).forEach(function (c) {
      typeof window[c] === "function" && !funcs[c] && delete window[c];
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM