我想在另一个函数里面动态地运行一个函数...
让我们说:
<script>
var x = function(r,s){
r(s);
}
var a = function(item){
alert("aA");
}
var b = function(item){
alert("bB");
}
</script>
Run Code Online (Sandbox Code Playgroud)
这可能吗?我从函数x的参数中取"r"作为函数名,我想运行它.
如果r = a,那么它将触发函数a()
如果r = b,则触发功能b ...
我怎么能这样做?
我试图在 if else 块之间放置一个函数。下面是我想象的。:
<script>
let condition=false
function function_here(event){
if(event){
condition = true;
}else if(!event){
condition = false;
}else{
condition = !condition;
}
</script>
{#each post as posts}
{#if posts.object1==="match" }
<p>HTML HERE</p>
{function_here(true)}
{/if}
{/each}
{#if condition}
<button>type1</button>
{:else}
<button>type2</button>
{/if}
Run Code Online (Sandbox Code Playgroud)
如果仅在循环发布结果期间,如果发现 posts.object1 匹配,我需要更改变量“条件”的状态
但是,此示例返回结果“未定义”。
我如何在 if 块之间放置函数?