javascript - 如果此代码运行则执行代码

标签 javascript if-statement

如果另一个代码片段已运行,我想运行代码。

如果此代码运行

(function() {
// Code runs here
})();

然后也运行此代码

//This code

示例

if (condition) {
    block of code to be executed if the condition is true
}

http://www.w3schools.com/js/js_if_else.asp https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/if...else

这似乎不起作用?

if ((function() {// Code runs here})();) 
    {
        //This code
    }

最佳答案

您应该使用 return 语句,否则 IIFE 将返回 undefined,因此它相当于 false 语句。

if (
(function() {
   // Code runs here
   return true;

})();
){
  //This code
}

关于javascript - 如果此代码运行则执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37137367/

相关文章:

java - 的解释?和 : operators

javascript - 如何检测css背景url

javascript - 在 jquery 上下文中调用 this.function

JavaScript LastChild 问题

javascript - 为什么reducer返回false时会返回undefined?

javascript - 带有位置服务的 wkwebview

javascript - Switch 语句实现未提供预期行为

if-statement - 没有 if 语句的分支

c - 短路评估评估 if( (a = 4) || (b = 6) || (c = 7) || (d = 8) )

if-statement - 为什么无论如何都要评估 ifelse 宏的 "wrong"表达式?