您可以使用哪种语言动态地动态重写功能? [英] In what languages can you dynamically rewrite functions on the fly?

查看:85
本文介绍了您可以使用哪种语言动态地动态重写功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我有必要动态地用javascript重写javascript函数.我做事的轻松,以及它的乐趣,令我震惊.

I recently had the necessity of rewriting a javascript function in javascript, dynamically. The ease with which I did it, and how fun it was, astounded me.

在这里,我有一些HTML:

Over here I've got some HTML:

<div id="excelExport1234" 
     onclick="if(somestuff) location.href='http://server/excelExport.aspx?id=56789&something=else'; else alert('not important');"
  >Click here to export to excel</div>

我无法更改输出的HTML,但是我需要向该链接添加一个额外的参数.我开始考虑,意识到我可以做到这一点:

And I couldn't change the outputted HTML, but I needed to add an extra parameter to that link. I started thinking about it, and realized I could just do this:

excelExport = $('excelExport1234');
if (needParam)
        eval('excelExport.onclick = ' + excelExport.onclick.toString().replace("excelReport.aspx?id", "excelReport.aspx?extraParam=true&id") + ';');
else
        eval('excelExport.onclick = ' + excelExport.onclick.toString().replace("extraParam=true&", "") + ';');

它像冠军一样运作! excelExport.onclick返回一个函数对象,该对象将转换为字符串,并对字符串进行一些操作.由于它现在是"function(){...}"的形式,因此我将其分配给dom对象的onclick事件.必须使用eval有点丑陋,但是AFAIK没有一个javascript函数构造函数可以接受一串代码并将其很好地变成一个对象.

And it worked like a champ! excelExport.onclick returns a function object which I convert to a string, and do some string manip on. Since it's now in the form of "function() { ... }", I just go back and assign it to the onclick event of the dom object. It's a little ugly having to use eval, but AFAIK there isn't a javascript function constructor that can take a string of code and turn it into an object nicely.

无论如何,我的意思不是我超级聪明(我不是),我的意思是这很酷.而且我知道javascript不是唯一可以做到这一点的语言.我听说Lisp出于这个确切的目的已经有多年的宏了.除了真正的grok宏之外,您还需要真正的grip lisp,而我不希望它的出现,我只是有点喜欢".

Anyway, my point isn't that I'm super clever (I'm not), my point is that this is cool. And I know javascript isn't the only language that can do this. I've heard that lisp has had macros for years for this exact purpose. Except to really grok macros you need to really grok lisp, and I don't grok it, I just 'kind of get it'.

所以我的问题是:您可以(轻松地)用其他哪些语言动态地重写函数,并且可以给我展示一个简单的示例吗?我想看看您还能在其他地方做到这一点,以及如何做完成了!

So my question is: In what other languages can you (easily) dynamically rewrite functions, and can you show me a simple example? I want to see where else you can do this, and how it's done!

(而且,我也不知道将其标记为什么,所以我随机猜测)

(also, I have no idea what to tag this as, so I took random guesses)

推荐答案

LISP是这方面的终极语言. LISP函数是实际的LISP列表,这意味着您可以像处理其他任何数据结构一样操作LISP源代码.

LISP is the ultimate language at this. LISP functions are actual LISP lists, meaning you can manipulate LISP source code as if it were any other data structure.

这是一个非常琐碎的示例:

Here's a very trivial example of how it works:

(define hi 
  (lambda () (display "Hello World\n")))
;; Displays Hello World
(hi)
(set! hi
      (lambda () (display "Hola World\n")))
;; Displays Hola World
(hi)

但是,这在函数是第一类对象的任何语言中都是可能的. LISP的这种语法功能最有趣的展示之一就是它的宏系统.我真的不认为我可以做到公正,因此,如果您有兴趣,请阅读以下链接:

This, however, is possible in any language where functions are first-class objects. One of the most interesting showcases of the power of this syntax for LISP is in its macro system. I really don't feel I could do the topic justice, so read these links if you're interested:

http://en.wikipedia.org/wiki/Macro_(computer_science) #Lisp_macros

http://cl-cookbook.sourceforge.net/macros.html

这篇关于您可以使用哪种语言动态地动态重写功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆