在javascript中将代码作为函数参数传递? [英] Pass code as function argument in javascript?

查看:51
本文介绍了在javascript中将代码作为函数参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自己创建一个javascript框架(所以没有jquery,mootools ...代码请),我想让我的框架的代码只能在框架函数中访问,例如,像这样:

I'm trying to create a javascript framework by myself(so no jquery,mootools... code please) and I want to make the code for my framework only accessible in a framework function so for example, something like this:

frameworkname({
//framework code here
});

因此我的框架代码不会与其他框架冲突。我知道 frameworkname({}); 是一个函数,但我不知道如何将代码作为函数参数传递。我知道这是可能的,因为我在jquery中很有经验,jquery到处都有这些东西(例如: $(document).ready(function(){// codehere}); ),但jquery开发人员是如何做到这一点的呢?我希望能够为我的框架做到这一点。任何帮助是极大的赞赏。

so my framework code doesn't conflict with other frameworks. I know that frameworkname({}); is a function, but I don't know how you pass code as a function argument. I know this is possible as I'm quite experienced in jquery and jquery has that stuff everywhere (example:$(document).ready(function(){//codehere});), but how did the jquery developers do this? I want to be able to do this for my framework. Any help is greatly appreciated.

推荐答案

函数是JavaScript中的第一类对象。您可以像字符串,数字,数组或任何其他数据类型一样传递它们。

Functions are first class objects in JavaScript. You can pass them around in the same way as Strings, Numbers, Arrays or any other data type.

var foo = function () { alert(1); };
var bar = function (arg) { arg(); }
bar(foo);

这篇关于在javascript中将代码作为函数参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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