简体   繁体   中英

JavaScript syntax error only for Visual Studio

For some reason, the following JS declaration causes a syntax error in Visual Studio and also fails to execute in Windows Phone browsers, but it's perfectly fine for other IDEs such as NetBeans.

The function comes from the Windy API Leaflet plugin Hello World example, and the syntax error is located in the line with the comment:

function initWindyMap(lat, lon, key, particlesAnim) {
    const options = {
        key: key,
        lat: lat,
        lon: lon,
        verbose: false,
        zoom: 6
    };
    windyInit(options, windyAPI => { 
        const {overlays, store, map} = windyAPI; /* syntax error on this line */
        store.set('lang', 'es');
        store.set('englishLabels', true);
        store.set('particlesAnim', particlesAnim);
        overlays.wind.setMetric('km/h');
        map.options.minZoom = 6;
        map.options.maxZoom = 6;
        map.dragging.disable();
        map.doubleClickZoom.disable();
        map.scrollWheelZoom.disable();
        L.marker(map.getCenter()).addTo(map);
    });
}

Here's a screenshot of the Visual Studio IDE for more details.

在此处输入图片说明

The question is: Is there a more orthodox way to declare this function and avoid this error? Surely this is a valid syntax in a new JS tweak released minutes ago for the Chrome webview 75 , but I need to use classic, standard JS syntax in this case... thanks in advance.

The question is: Is there a more orthodox way to declare this function and avoid this error?

It's an arrow function. They've been standard JavaScript syntax since 2015. They're supported in all vaguely-modern browsers (so not IE11).

You can use a traditional function instead:

windyInit(options, function(windyAPI) {
    // ...
});

Since yours function doesn't use this , there's nothing else you need to do other than just changing the syntax from arrow to traditional. (One of the differences between arrow functions and traditional functions is that arrow functions close over this , rather than having this set by how they're called. More about arrow functions here .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM