Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

javascript - Fix error in spectrum color picker

I implemented spectrum color picker, and I'm trying to fix up the JSLint errors. I have 2 types of errors which I can't seem to fix. Here are the errors

  • 'var' was used before it was defined

  • Move the invocation into the parens that contain the function

Here's the code with the errors:

(function (factory) {
    "use strict";

    if (typeof define === 'function' && define.amd) { // AMD
        define(['jquery'], factory);
    } else if (typeof exports === "object" && typeof module === "object") { // CommonJS
        module.exports = factory;
    } else { // Browser
        factory(jQuery);
    }
})(function($, undefined) {
    "use strict";
   ...

define, exports, and module all have the error that it's not defined.

Then the second function: })(function($, undefined) { has the 2nd error mentioned above. So I checked up that error, and I tried what it said: }(function ($, undefined) ) { I moved the closing parenthesis to the end, and I now get the following error:

Expected '{' and instead saw '}'.

How can I fix the 2 errors mentioned above?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Converting })(function($, undefined) { into }(function ($, undefined) ) { is erroneous because you need to move the {} inside the parentheses as well, like so:

(function() {
    ...
}(function() { ... }));

With regards to the undefined errors you're getting, just put this comment at the top of your JS file:

/*global define, module, exports*/

Feel free to add some other common variables if you need to exclude them as well:

/*global define, module, exports, document, window, alert, console, require*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...