gpt4 book ai didi

javascript - 破解 JS : Thoughts on mixing in some object as arguments (or inner variables) for external functions (like **kwargs) w/o using this

转载 作者:行者123 更新时间:2023-11-30 18:32:33 25 4
gpt4 key购买 nike

在 SO 上确实有很多问题看起来与此类似,但实际上每个问题都与不同的事情有关。而且我知道,我描述的是一种黑客攻击和不推荐的方式,但在我的情况下它确实是必需的(代码大小对我来说真的很重要,每五个字节很重要):

我拥有的是动态填充一些变量的对象(我准备它供内部使用,但它填充了用户定义的变量):

var scope = { 'a': 0, 'b': 12, 'c': 64 };

而且,我有一个用户定义的匿名函数,它需要直接访问这个范围,而不使用 this(我真的很喜欢它),它被定义为 在我的 var-scope 中:

function anonymousFunc() { // user don't knows any names of arguments
console.log(a, b, c); // I expect '0, 12, 64'
}

事实上,scope 甚至不是一个对象,而是一个存储上下文级别的复杂数组,所以说实话,实际上它看起来更像这样:[ { 'a' : 0 }, { 'b': 12, 'c': 64 } ],然后我选择要访问的具体函数的级别。而且,更真实地说,我有几个这样的对象混合到一个函数范围中。

所以最好的另一种不进行黑客攻击的方法(evalwith)(OOP 方式)我明白了,就是让用户像这样:

(function() {

function prepareAndCall(f) {
c = this.ctx[level];
l = currentLabels();
m = makeChunk();
f();
}

var c, l, m;

// it is injected from user code
function anonymousFunc() {
console.log(c.a, c.b, l.g, m.pos);
}

prepareAndCall(anonymousFunc);

})();

但我想要的是让用户不必考虑他需要在哪里获取这些变量。我可以将它们混合在一个对象中,但在我的情况下这也不是一件合适的事情,这些变量中的大多数都是用户自己在外部位置定义的,所以他希望直接将它们放在他的范围内,我'我喜欢他对那些 c-m-l 变量一无所知,即使它只是一个 c。我想模仿他手头有这些变量。他知道他可能会不小心重新定义它们。


于是,我找到了一些方法:

一个With eval, and it requires a string-source of function

(更新。,我删除了 previous bad example @Gijs 评论并用新评论更新)

第二个是污染全局环境 ( at jsfiddle )

(function(global_ctx) { 

// here is the user context, variables
// defined here are accessible to user

var __g = global_ctx;
__g.__test_var = 5;
if (__test_var !== 5) alert('__g is not global context');

var __f = (function() {

// here is the inner context, variables
// defined here are not accessible to user

var scope = [
{ a: 16 },
{ oo: 12 },
{ b: 17, c: 20 }
];

function callUserFunc(f) {
//console.log('call user func');
loadVars(__g, 2);
f();
}
// __g.__f = callUserFunc;

function loadVars(g, level) {
for (name in scope[level]) {
g[name] = scope[level][name];
}
}

return callUserFunc;

})();

if (typeof scope !== 'undefined') alert('scope is visible, error');
if (typeof loadVars !== 'undefined') alert('loadVars is visible, error');

__f(function() { // user function will be inserted here
if (typeof oo !== 'undefined') alert('user function sees what it must not see');
alert(b + ', ' + c);
});

})(this);

因此,我认为这可能不是一个问题,而是一个需要讨论的主题:使用 witheval 真的是一种糟糕的方式吗 在这种情况下?因为我不知道还有什么其他方法可以这样做。

相关文献:

最佳答案

基本上,您要做的是修改用户给定函数运行的范围。问题是函数的范围 cannot be modified (在链接中:“无法从您的 JavaScript 代码直接访问这些范围对象”);您可以使用 .call.apply 修改 this 的绑定(bind),但这与修改封闭范围不同。

据我所知,这条规则只有三个异常(exception):

  1. 全局作用域对象(在浏览器环境中,通常是 window,或 DOM Worker 作用域 (self) (!))。因为这是全局范围,所以您知道它在哪里并且可以更改它,对它的更改将影响在同一全局范围内运行的代码。
  2. with block 中声明它 outside of ES5 strict
  3. 使用eval outside of ES5 strict

实际上,如果用户定义的代码不需要访问 DOM,DOM Workers 可能会有所帮助。在 worker 中,全局范围被标识为 self。不幸的是,postMessage 仅支持字符串和 JSON 可序列化对象,这意味着函数已过时。这意味着您仍然必须传递一个字符串,然后用它调用 new Functioneval。好处是你相对隔离于用户生成的代码会弄乱你自己的页面的任何事情(当然这确实意味着你需要不信任你在 onmessage 中获得的任何东西onerror 处理程序...)。

关于javascript - 破解 JS : Thoughts on mixing in some object as arguments (or inner variables) for external functions (like **kwargs) w/o using this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9192950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com