【问题标题】:Is this a legit javascript syntax? [duplicate]这是合法的javascript语法吗? [复制]
【发布时间】:2014-12-02 10:14:53
【问题描述】:

我想问一下下面的函数声明是否正确,这个语法是否可行。

(function(){



$(document).on("click", ".MyButton", function(event)
{
    if ($(this).attr("id") == "ButtonAuftrag")
    {
        $.mobile.changePage("#pagetwo");

    }
}
)();

该功能应在单击按钮时触发。我用谷歌搜索,从来没有找到这样的语法。 第一行 (function(){ 和函数的关闭让我感到困惑。

【问题讨论】:

  • 您发布的代码中确实存在语法错误(括号和花括号没有正确平衡)。
  • 不,因为它应该是这样的:jsfiddle.net/RokoCB/dypo5czr
  • 我问这个是因为我试图理解代码。
  • 脚本不起作用,我想找出错误。 + 我不想在这里发布漏洞脚本,因为我想做的工作

标签: javascript function syntax


【解决方案1】:

需要注意的事情很少

(function() {//<-- its function without name
  $(document).on("click", ".MyButton", function(event) {
    if (this.id === "ButtonAuftrag") {//<-- updated this line
      $.mobile.changePage("#pagetwo");
    }
  });//<-- added this line
})();//<-- executing the block declared
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

示例,名称:

var run = function() { //<-- named as `run`
  console.log('running...');
}; //<-- not invoked

console.log('after run() declared ...');
run(); //<-- invoked
console.log('run() executed ...');
open console... F12

示例,无名称:

(function() {
  console.log('runner 1 ...');
}); //<-- not invoked

console.log('after runner 1 declared ...');

(function() {
  console.log('runner 2 ...');
})(); //<-- invoked

console.log('after runner 2 declared ...');

(function(x) {
  console.log('runner 3 ...', 'x: ', x);
})(56); //<-- invoked with parameters

console.log('after runner 3 declared ...');
open console... F12

【讨论】:

  • 您未能在此处解决实际问题:"(function(){ and the closing of the function is confusing me."
  • 语法问题是个问题 - 是的,但您没有回答问题。
  • 我无法知道您发布了一个不完整的答案,打算更新/改进它:) 顺便说一句,投票不是我的
  • @lix,我更新了我的帖子
【解决方案2】:

这称为自调用匿名函数或IIFE (Immediately Invoked Function Expression)

虽然存在一些轻微的语法问题,但我将其归因于错误的复制粘贴到站点中。请查看解决此问题的其他答案。

基本上这里发生的事情是,当整个函数被括在括号中时,您将其定义为一个表达式;类似表达式:

var a = ( 3 == 5 )

这将使用值 false 填充变量 a。以类似的方式,在用括号包裹函数后,该函数实际上被称为()。所以基本上这个函数一定义就被调用。

【讨论】:

  • 除非你错过了这个缺少的右括号
  • @Jamiec - 我假设(正如我所说),语法错误只是复制/粘贴问题。这里的 main 问题是自调用函数;它有效的代码。
【解决方案3】:

http://en.wikipedia.org/wiki/Immediately-invoked_function_expression

只是为了提前修复一些错误

(function(){

  $(document).on("click", ".MyButton", function() { // You don't use any event
    if ( this.id == "ButtonAuftrag"){ // Use this force Luke
        $.mobile.changePage("#pagetwo");
    }
  }); // Close properly

}()); // Close properly

fiddle

叫做IIFE立即调用函数表达式

(function(){
      // Function Scope's Code here...
      // Declared variables become private and
      // not accessible from outside the fn scope
}()); // the () here will expose to execution the function content

简单示例:

(function(){
   console.log("HEYYY");
   a = "a";     // Global variable (in window Object)
}());           // "HEYYY" 
console.log(a); // "a" // accessible


(function(){
  console.log("HEYYY");
  var b = "b";  // Private variable 
}());            
console.log(b); // referenceError : b is not defined

【讨论】:

【解决方案4】:

不,您没有关闭外部函数。你想要这个吗?

(function(){
    $(document).on("click", ".MyButton", function(event) {
        if ($(this).attr("id") == "ButtonAuftrag"){
            $.mobile.changePage("#pagetwo");
        }
    })
}());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    相关资源
    最近更新 更多