【问题标题】:Is it me or Visual Studio 2010?是我还是 Visual Studio 2010?
【发布时间】:2011-08-02 15:42:10
【问题描述】:

每当我为网站编写大型 JavaScript 库时,我都会使用以下模式。

虽然在运行时一切看起来都很好,但在 Visual Studio 中总是会做噩梦。

在匿名函数表达式的最后一行我总是得到错误

“预期;”

在最后的右括号上

}(窗口,jQuery));

我通过 jslint 运行代码没有任何问题,但我的智能感知总是中断,我无法格式化代码。我错过了什么吗?

; (function (window, $) {

    // Define a local copy of MyLibrary
    var MyLibrary = {},

    // Shortcuts.
    // A central reference to the root messages object
    $$messages,

    // A central reference to the root messages.messageType object
    $$messageType;

    MyLibrary = function () {
        // The MyLibrary object is actually just the init 
        // constructor 'enhanced'
        return new MyLibrary.fn.init();
    };

    MyLibrary.fn = MyLibrary.prototype = {
        init: function () {
            // Initialise the object shortcuts.
            $$messages = MyLibrary.fn.messages;
            $$messageType = MyLibrary.fn.messages.messageType;
        }
    };

    // Give the init function the MyLibrary prototype for later instantiation
    MyLibrary.fn.init.prototype = MyLibrary.fn;

    MyLibrary.fn.messages = {
        /// <summary>
        /// Provides means to provide feedback message to the client.
        /// </summary>
        messageType: {
            information: "information",
            error: "error",
            success: "success"
        }
    };

    MyLibrary.fn.tester = function () {
        alert($$messageType.success);
    };

    // Expose MyLibrary to the global object
    window.MyLibrary = window.$m = MyLibrary();

} (window, jQuery));

jQuery(document).ready(function () {
    $m.tester();
});

【问题讨论】:

  • 您看到声明开头的;了吗?
  • (function (window, $) { ... } (window, jQuery)); 我觉得应该是(function (window, $) { ... }) (window, jQuery);
  • @Raynos:你说得对,它是最初的“;”它可以保护自己免受任何其他不以一个结尾的脚本的关闭。将其粘贴在答案中,我会将您标记为正确答案。 Shame VS 不够聪明,无法确定这一点。 @Cem:语法可以以任何一种形式使用,但是我的语法是 crockford 推荐的语法,因为它更清楚地展示了表达式的范围。不过还是谢谢。
  • 'mark down' 对你来说可能意味着一件好事?
  • @Sehe:呵呵呵呵...英语的精妙之处嗯?

标签: javascript jquery visual-studio-2010


【解决方案1】:

; 可能会导致错误。不过我不知道为什么会在开头。

【讨论】:

  • 一开始是为了保护自己免受其他脚本的伤害...这里解释一下为什么...stackoverflow.com/questions/2481543/…
  • @JamesSouth 我知道你的意思。我更喜欢“精英”的态度,如果你将我的脚本与错误的代码连接起来,那不是我的问题。
【解决方案2】:

} (window, jQuery));

应该是

})(window, jQuery);

【讨论】:

  • 这实际上是一个相当主观的问题。我将它用作 Crockford 模块化模式的一部分。查看这篇关于闭包的文章,它给出了大约一半的解释。 james.padolsey.com/javascript/closures-in-javascript
  • 这两种方法都是正确的。对于它的价值,Crockford 建议第一个。
  • 我已经查看了那篇文章和 yuiblog 上的模块化模式链接,也许我错过了一些东西,但我从来没有看到它说你应该把你的自调用括号放在不必要的函数中包装括号。在这两篇文章中,我看到的每个实例都将自调用括号放在函数包装括号之后。显然,无论哪种方式都有效。但是函数包装括号存在的全部原因是表明它后面的括号是自调用的。因此,将自我调用的括号放在函数包装括号中有点违背了目的,imo。
  • @scott:是的,我的错误,链接的文章描述了闭包模式,但它只是在 cmets 的下方提到了不同的括号模式。我完全可以看到您来自哪里,但我无法想象为什么 Crockford 会在没有正当理由的情况下将其指定为需要注意的东西。
  • @James - 是的,我也不知道。不过我会告诉你,我看到 Paul Irish 使用它,这是我最喜欢的模式:(function($, window, document, undefined) { ... })(jQuery, this, document);。他提到的真正酷的事情是,通过不传递 undefined 值,默认情况下 undefined 将真正是未定义的。因此,在你的函数内部使用 undefined 进行比较将获得所达到的结果。它可以防止有人将undefined = null; 放在您的代码之上并抛出未定义的比较。无论如何,我想我只是将其作为旁注提及。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 2011-10-05
  • 2010-12-13
相关资源
最近更新 更多