javascript - 如何在不同的函数中访问字符串
问题描述
我想在下面的代码中访问函数中的entityString
值。具有以下属性,我想从中访问函数中的状态值。updateMarkerIcon
entityString
updateMarkerIcon
id:3 号楼
类型:MetroStation
地址:Kashmiri_GateMetro纬度:
28.6674
经度:77.2282
房间数:6
状态:危急
状态:温度高 --
function() {
"use strict";
var icon;
MashupPlatform.wiring.registerCallback("entityInput", function(entityString) {
var entity = JSON.parse(entityString);
var coordinates = null;
var coord_parts = null;
var coordinates_pref = MashupPlatform.prefs.get('coordinates_attr');
var attributes = coordinates_pref.split(new RegExp(',\\s*'));
if (attributes.length < 1) {
return;
} else if (attributes.length >= 2 && entity[attributes[0]] != null && entity[attributes[1]] != null) {
coord_parts = [
entity[attributes[0]],
entity[attributes[1]]
];
} else if (entity[attributes[0]]) {
coord_parts = entity[attributes[0]].split(new RegExp(',\\s*'));
}
if (coord_parts != null && coord_parts.length === 2) {
coordinates = {
system: "WGS84",
lat: parseFloat(coord_parts[0]),
lng: parseFloat(coord_parts[1])
};
}
if (coordinates) {
MashupPlatform.wiring.pushEvent("poiOutput", JSON.stringify(entity2poi(entity, coordinates)));
}
});
var entity2poi = function entity2poi(entity, coordinates) {
var poi = {
id: entity.id,
icon: icon,
tooltip: entity.id,
data: entity,
infoWindow: buildInfoWindow.call(this, entity),
currentLocation: coordinates
};
return poi;
};
var internalUrl = function internalUrl(data) {
var url = document.createElement("a");
url.setAttribute('href', data);
return url.href;
};
var buildInfoWindow = function buildInfoWindow(entity) {
var infoWindow = "<div>";
for (var attr in entity) {
infoWindow += '<span style="font-size:12px;"><b>' + attr + ": </b> " + entity[attr] + "</span><br />";
}
infoWindow += "</div>";
return infoWindow;
};
var updateMarkerIcon = function updateMarkerIcon() {
icon = MashupPlatform.prefs.get('marker-icon');
if (icon == '') {
icon = internalUrl('images/icon.png');
}
};
MashupPlatform.prefs.registerCallback(updateMarkerIcon);
// Init initial marker icon
updateMarkerIcon();
}
解决方案
您必须更改您的updateMarkerIcon()
函数以接受一个参数e.g. var updateMarkerIcon = function updateMarkerIcon(<someParameterHere>)
,然后在您的.registerCallback()
回调函数中调用该函数,并传递entityString
给它e.g. updateMarkerIcon(entityString)
推荐阅读
- java - 在日期和时间之间用“T”解析时间戳
- python - 如何检查熊猫行中的连续差异
- java - 如何在java中转换泛型方法的返回?
- powershell - CIM、Win32_OperatingSystem 和 Win32ShutdownTracker
- oop - 从 OOP 的角度来看,在接口中声明类型/属性是否正确?
- ionic4 - 是否可以在离子选择器中自动选择并立即跳转到某个值?
- r - 将 R 中的多个 RDS 文件作为自定义函数中的多个对象加载
- excel - 如何使用vba从包含行索引的数组中选择特定行
- scala - scalaj Http标头未被识别
- c# - 天蓝色物联网设备到云消息传递