关于浏览器javascript引擎的理论问题

发布于 2024-08-24 20:45:41 字数 277 浏览 1 评论 0原文

我怀疑这是可能的,而且我确信在生产环境中这样做将是一个非常糟糕的主意。这只是那些假设的“我想知道我是否可以……”的事情之一。

我想知道是否可以修改或扩展浏览器 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以酷 2024-08-31 20:45:41

我对此表示怀疑。

解析和执行 javascript 的引擎位于客户端的浏览器上,因此任何网站都不能修改或更改(我希望)。

您可能可以使用 javascript 支持的类型和语法来描述 lambda 表达式,然后拥有自己的 javascript 库,将其扩展为有效的 javascript 调用。

然而,它不会那么有用,因为 javascript 函数已经非常灵活了。上面有效 JS 中的代码看起来就像等效的 c# 委托:

var x = myJsObjCollection.Where(function() { if (this.ID == 2) return this; });

这不需要输入更多工作。

更新

采取鲍勃的想法再进一步,您可能会编写如下内容:

function lambda(vName, comparison)
{
    var exp = new RegExp("\\b" + vName + "\\.", "g");
    comparison = comparison.replace(exp, "arg.");
    return function(arg) {
        var result;
        eval("result = " + comparison + ";");
        return result;
    };
}

然后您的 Where 函数将类似于:

Array.prototype.Where = function(lambdaFunc) {
    var matches = [];    
    for (var i in this)
    {
        if (lambdaFunc(this[i]))
            matches[matches.length] = this[i]
    }
    return matches;
};

您可以调用它:

var x = myJsObjCollection.Where(lambda("a", "a.ID == 2"));

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:

var x = myJsObjCollection.Where(function() { if (this.ID == 2) return this; });

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:

function lambda(vName, comparison)
{
    var exp = new RegExp("\\b" + vName + "\\.", "g");
    comparison = comparison.replace(exp, "arg.");
    return function(arg) {
        var result;
        eval("result = " + comparison + ";");
        return result;
    };
}

Then your Where function would look something like:

Array.prototype.Where = function(lambdaFunc) {
    var matches = [];    
    for (var i in this)
    {
        if (lambdaFunc(this[i]))
            matches[matches.length] = this[i]
    }
    return matches;
};

And you could call it:

var x = myJsObjCollection.Where(lambda("a", "a.ID == 2"));

Working example at http://jsbin.com/ifufu/2/edit.

ˉ厌 2024-08-31 20:45:41

在 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 :)

潇烟暮雨 2024-08-31 20:45:41

答案几乎是“不”,但请查看: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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文