关于浏览器javascript引擎的理论问题
我怀疑这是可能的,而且我确信在生产环境中这样做将是一个非常糟糕的主意。这只是那些假设的“我想知道我是否可以……”的事情之一。
我想知道是否可以修改或扩展浏览器 JavaScript 引擎在运行时解析代码的方式。例如:如果我尝试在 JavaScript 中使用 C# 风格的 lambda 语法:
var x = myJsObjCollection.Where(a=>a.ID == 2);
我可以修改脚本的解析并拦截 lambda 代码并自行解析吗?
I doubt this is possible, and I'm certain to do it in a production environment would be an impressively bad idea. This is just one of those hypothetical "I-wonder-if-I-could..." things.
I was wondering if it is possible to modify or extend how the browser JavaScript engine parses the code at run-time. For example: if I tried to use C# style lambda syntax in JavaScript:
var x = myJsObjCollection.Where(a=>a.ID == 2);
Could I modify the parsing of the script and intercept the lambda code and parse it myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对此表示怀疑。
解析和执行 javascript 的引擎位于客户端的浏览器上,因此任何网站都不能修改或更改(我希望)。
您可能可以使用 javascript 支持的类型和语法来描述 lambda 表达式,然后拥有自己的 javascript 库,将其扩展为有效的 javascript 调用。
然而,它不会那么有用,因为 javascript 函数已经非常灵活了。上面有效 JS 中的代码看起来就像等效的 c# 委托:
这不需要输入更多工作。
更新
采取鲍勃的想法再进一步,您可能会编写如下内容:
然后您的 Where 函数将类似于:
您可以调用它:
http://jsbin.com/ifufu/2/edit。
I doubt it.
The engine which parses and executes the javascript is on the client's browser, and as such cannot be modified or changed by any website (I would hope).
Potentially you could use javascript supported types and syntax to describe a lambda expression and then have your own javascript library which expands it to valid javascript calls.
However, it would not be that useful since javascript functions are already super flexible. Your code above in valid JS would look like the equivalent c# delegate:
Which isn't a whole lot more work to type.
Update
To take Bob's Idea a couple steps further, you could potentially write something like this:
Then your Where function would look something like:
And you could call it:
Working example at http://jsbin.com/ifufu/2/edit.
在 JavaScript 中没有直接的方法可以做到这一点。最接近的方法是编写自己的 eval 类型函数,该函数可以解释您想要的任何代码。
或者,获取 V8 JavaScript 引擎源代码,对其进行一些更改,然后看看是否可以以某种方式在 Chrome 中实现它:)
There's no direct way to do this in JavaScript. The closest you could get would be to write your own
eval
type function which would interpret whatever code you want.Or, get the V8 JavaScript engine source code, make some changes to that, and see if you can get it implemented in Chrome somehow :)
答案几乎是“不”,但请查看:http://osteele.com/sources /javascript/function/ 特别是有关字符串的内容 ->功能“强制”。
The answer is pretty much "no", but check this out: http://osteele.com/sources/javascript/functional/ and in particular the stuff about string -> function "coercion".