Warning JSHint with backbone : Expected an assignment or function call and instead saw an expression

StackOverflow https://stackoverflow.com/questions/23308974

  •  09-07-2023
  •  | 
  •  

Вопрос

On all my JS files, i have a warning of JSHint : Expected an assignment or function call and instead saw an expression.

A example :

/*global App, Backbone*/

App.Routers = App.Routers || {};

(function () {
    'use strict';

    App.Routers.ApplicationRouter = Backbone.Router.extend({

        routes: {
            '': 'home',
            '!/case-projet/:projet(/)': 'projetService',
            '!/case-projet/:projet/Galerie(/)': 'galerieService',
            '!/case-projet(/)': 'homeEvents',
        },
        projetService: function (e) {
            this.loadView(e), App.activeProjectView.enterFromRouter();
        },
        galerieService: function (e) {
            this.loadView(e), App.activeProjectView.enterGalleryFromRouter();
        },
        homeEvents: function (e) {
            this.loadView(e),
            $('html,body').animate({scrollTop: $('.line[data-id="events"]').offset().top}, 2000, 'easeInOutQuint');
        },
        home: function () {
            new App.Views.Homeview();
        },
        loadView: function (e) {
            switch (e) {
            case 'Homeview':
                App.loadView(new App.Views.Homeview());
                break;
            case 'Incentive':
                App.loadView(new App.Views.Incentive());
                break;
            case 'Corporate':
                App.loadView(new App.Views.Corporate());
                break;
            case 'Convention':
                App.loadView(new App.Views.Convention());
                break;
            case 'Studio':
                App.loadView(new App.Views.Studio());
                break;
            default:
                App.loadView(null);
            }
        }

    });

})();

on http://www.jshint.com/ i have :

Four warnings 17 Expected an assignment or function call and instead saw an expression. 20 Expected an assignment or function call and instead saw an expression. 24 Expected an assignment or function call and instead saw an expression. 27 Do not use 'new' for side effects

Thanks

Это было полезно?

Решение

You haven't said where you're getting these errors, but for instance here you're using the comma operator for no good reason:

this.loadView(e), App.activeProjectView.enterFromRouter();

Just do:

this.loadView(e);
App.activeProjectView.enterFromRouter();

Re new with side effects, it's complaining about this:

new App.Views.Homeview();

There, new is being used to construct an object, but then you're not saving the object reference (the result of the new expression) anywhere. So it's only being used for side-effects (other things that happen in the function), which is not generally a good idea.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top