我在javascript中有一个函数:
function alertMe($a)
{
alert($a);
}
Run Code Online (Sandbox Code Playgroud)
我可以这样执行:alertMe("你好");
我想要做的就是用"你好"参数变量$ FUNC分配alertMe("你好"),并能够通过执行类似$ FUNC后来执行这个();
Dee*_*ons 11
我想将评论添加为答案
//define the function
function alertMe(a) {
//return the wrapped function
return function () {
alert(a);
}
}
//declare the variable
var z = alertMe("Hello");
//invoke now
z();
Run Code Online (Sandbox Code Playgroud)
只需构建您需要的函数并将其存储在变量中:
var func = function() { alertMe("Hello") };
// and later...
func();
Run Code Online (Sandbox Code Playgroud)
如果您想改变字符串,您甚至可以创建一个函数来构建您的函数:
function buildIt(message) {
return function() { alertMe(message) };
}
var func1 = buildIt("Hello");
var func2 = buildIt("Pancakes");
// And later...
func1(); // says "Hello"
func2(); // says "Pancakes"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4332 次 |
| 最近记录: |