- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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 } ]
,然后我选择要访问的具体函数的级别。而且,更真实地说,我有几个这样的对象混合到一个函数范围中。
所以最好的另一种不进行黑客攻击的方法(eval
或 with
)(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);
因此,我认为这可能不是一个问题,而是一个需要讨论的主题:使用 with
或 eval
真的是一种糟糕的方式吗 在这种情况下?因为我不知道还有什么其他方法可以这样做。
相关文献:
最佳答案
基本上,您要做的是修改用户给定函数运行的范围。问题是函数的范围 cannot be modified (在链接中:“无法从您的 JavaScript 代码直接访问这些范围对象”);您可以使用 .call
和 .apply
修改 this
的绑定(bind),但这与修改封闭范围不同。
据我所知,这条规则只有三个异常(exception):
window
,或 DOM Worker 作用域 (self
) (!))。因为这是全局范围,所以您知道它在哪里并且可以更改它,对它的更改将影响在同一全局范围内运行的代码。with
block 中声明它 outside of ES5 strict eval
outside of ES5 strict 实际上,如果用户定义的代码不需要访问 DOM,DOM Workers 可能会有所帮助。在 worker 中,全局范围被标识为 self
。不幸的是,postMessage
仅支持字符串和 JSON 可序列化对象,这意味着函数已过时。这意味着您仍然必须传递一个字符串,然后用它调用 new Function
或 eval
。好处是你相对隔离于用户生成的代码会弄乱你自己的页面的任何事情(当然这确实意味着你需要不信任你在 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/
例如,如果我的程序名称是 test.c 然后对于以下运行命令,argc = 2 而不是 4。 $test abc pqr* *xyz* 最佳答案 尝试运行: $ echo abc pqr* *xyz*
我正在尝试使用一个容器来显示TextField,但是该容器不喜欢我的操作顺序。这是我的代码: Widget build(BuildContext context) { return Scaffol
我有以下代码: class MetricGoogleGateway extends AMetricGateway{ constructor(id, name, token) {
我像这样调用下面的对象方法。 new Cout( elem1 ).load( 'body' ) new COut( elem1 ).display( 'email' ) 我一次只使用一个实例。因为我一
我正在尝试使用 C++11 中的可变参数函数模板,并通过如下代码了解了基本思想: void helper() { std::cout void helper( T&& arg ) {
在学习 ExtJS 4 时,我发现在定义一个新类时,在 initComponent 中方法可以使用 this.callParent(arguments) 调用父类的构造函数. 我想知道这个 argum
使用 XCode 9,Beta 3。Swift 4。 statsView.createButton("Button name") { [weak self] Void in //stuff st
以下代码将打印1: (function (arguments) { console.log(arguments); }(1, 2)); 实际上,arguments 对象已被覆盖。是否可以恢复函
/** * @param $name * @return Response * @Route ("/afficheN/{name}",name="afficheN") */ public fu
我习惯使用Scala scopt用于命令行选项解析。您可以选择参数是否为 .required()通过调用刚刚显示的函数。 如何定义仅在定义了另一个参数时才需要的参数? 例如,我有一个标志 --writ
所以这是我的代码: def is_valid_move(board, column): '''Returns True if and only if there is an o
我试图在这里运行此代码: threads = [threading.Thread(name='ThreadNumber{}'.format(n),target=SB, args(shoe_type,m
在静态类型函数编程语言(例如 Standard ML、F#、OCaml 和 Haskell)中,编写函数时通常将参数彼此分开,并通过空格与函数名称分开: let add a b = a + b
function validateArguments(args) { if(args.length 2) { throw new RangeError("Invalid amo
我正在使用 Django 1.5 并尝试将参数传递到我的 URL。当我使用前两个参数时,下面的代码工作正常,使用第三个参数时我收到错误。我已经引用了新的 Django 1.5 更新中的 url 用法,
我刚刚开始使用 ember js 并且多次被这个功能绊倒 有人可以简要介绍一下 this._super() 的使用,并解释 ...arguments 的重要性 谢谢 最佳答案 每当您覆盖类/函数(例如
这个问题在这里已经有了答案: How to fix an "Argument passed to call that takes no arguments" error? (2 个答案) 关闭 3
我正在创建一个简单的登录注册应用程序。但是我遇到了错误,我不知道如何解决,请帮忙!这是我的代码: // // ViewController.swift // CHLogbook-Applicati
我是 Swift 的初学者。我尝试创建一个表示 Meal 的简单类。 它有一些属性和一个返回可选的构造函数 但是当我尝试测试它或在任何地方实例化它时,我得到的只是一个错误。似乎无法弄清楚发生了什么。
我有一个在特殊环境下运行其他程序的系统程序: cset shield -e PROGRAM .现在要运行一个 java 程序,我输入了 cset shield -e java PROGRAM ,但这不
我是一名优秀的程序员,十分优秀!