简体   繁体   English

Javascript在匿名函数中分配变量的值

[英]Javascript assign value of variable inside anonymous function

I'm trying to create several layers for OpenLayers. 我正在尝试为OpenLayers创建几层。 I loop through the array and assign values for each values in using typename: 'Test:' + item.ServiceName . 我遍历数组,并使用typename: 'Test:' + item.ServiceName为每个值分配值。 The problem is when the OpenLayer calls the function it grabs the last assigned value of item.ServiceName which is always "Test_Layer_3" so Test_Layer_1 and Test_Layer_2 are not used. 问题是当OpenLayer调用该函数时,它将获取item.ServiceName的最后一个分配值,该值始终为“ Test_Layer_3”,因此不使用Test_Layer_1和Test_Layer_2。

How can I change it so that Test_Layer_1, Test_Layer_2, Test_Layer_3 are assigned to typename? 如何更改它,以便将Test_Layer_1,Test_Layer_2,Test_Layer_3分配给typename?

    var _MyLayers = [];
    _MyLayers.push({ "Id": 0, "ServiceName": "Test_Layer_1" });
    _MyLayers.push({ "Id": 1, "ServiceName": "Test_Layer_2" });
    _MyLayers.push({ "Id": 2, "ServiceName": "Test_Layer_3" });

    for (var i = 0; i < _MyLayers.length; i++)
    {
        var item = _MyLayers[i];

        var sourceVector = new ol.source.Vector({
            loader: function (extent)
            {
                $.ajax('http://localhost/geoserver/wfs', {
                    type: 'GET',
                    data: {
                        service: 'WFS',
                        version: '1.1.0',
                        request: 'GetFeature',
                        typename: 'Test:' + item.ServiceName,
                        srsname: 'EPSG:3857',
                        outputFormat: 'application/json',
                        bbox: extent.join(',') + ',EPSG:3857'
                    }
                });
            },
            strategy: ol.loadingstrategy.tile(new ol.tilegrid.createXYZ({})),
        });
        var layerVector = new ol.layer.Vector({
            source: sourceVector,
            minResolution: 0,
            maxResolution: 2,
            style: new ol.style.Style({ stroke: new ol.style.Stroke({ color: "rgba(25, 163, 255, 1.5)", width: 1 }) })
        });

        _MapLayers.push(layerVector);
    }

It should work by replacing item.ServiceName with _MyLayers[i].ServiceName . 它应该通过用_MyLayers[i].ServiceName替换item.ServiceName来工作。

Explanation 说明

When you define the loader functions it does not mean item.ServiceName is assigned to typename at that moment. 定义loader功能时,并不表示item.ServiceName会分配给typename Instead the function maintains a link to your item variable (sort of), which then links back to the value of _MyLayers[i] — a global. 取而代之的是,该函数会维护一个指向您的item变量(某种)的链接,该链接然后又链接回_MyLayers[i]的值—全局值。

When the loader function is called for each object by OpenLayers, 'Test:' + item.ServiceName, is evaluated and assigned to typename where the value represented by item has since changed. 当OpenLayers为每个对象调用loader函数时,将对'Test:' + item.ServiceName,进行评估,并将其分配给typename ,其中item表示的值此后已更改。 It will now point to the last item (eg _MyLayers[3] ) since it was the last value item was assigned. 现在它将指向最后一个项目(例如_MyLayers[3] ),因为它是最后一个值item

By replacing the line above, the loader functions now each maintain a unique link directly to the global _MyLayers . 通过替换上面的行, loader函数现在每个都维护一个直接链接到全局_MyLayers的唯一链接。 As i is a number, each loader function implements a statement based on the value of i at the time the function was defined in your for loop (eg _MyLayers[1] , _MyLayers[2] , ... and so on). 由于i是数字,因此每个加载器函数都会在for循环中定义该函数时基于i的值(例如_MyLayers[1]_MyLayers[2]等)实现一个语句。

I ended up assigning the value of item.ServiceName to sourceVector. 我最终将item.ServiceName的值分配给sourceVector。 Then reading the value of sourceVector inside the loader function. 然后读取loader函数中的sourceVector的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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