- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
下面的示例工作正常,但是,我试图使函数 handleArcs()
更通用(即 handleLayer()
)。
我的 layerVar
有一个属性 onEachFeature
,它将 onEachArc
方法应用于图层的每个特征。我希望 handleArcs()
将函数 onEachArc()
作为参数,但它不起作用,当我传递它并使用 typeof
进行检查时>,结果为 undefined
。基本上它是将一个函数作为参数传递给另一个函数的简单方法,但在这种情况下不起作用。
我的第一个假设是上下文 this
有问题。但是因为 typeof thisShouldBeAFunction
返回 undefined
我现在不确定问题是什么。
任何猜测可能是问题的原因是什么?
function onEachArc(feature, layer) {
if (feature.properties) {
layer.bindPopup("<b>" + feature.properties.linkstr + "</b> has speed " + feature.properties.speed + ".");
}
};
function handleArcs(data, layerVar, layerName, thisShouldBeAFunction) {
alert(typeof thisShouldBeAFunction);
//Add the feature to the map
layerVar = new L.geoJson(data, {
onEachFeature: onEachArc
}).addTo(map);
}
getData('Layers/arcs.json').done(handleArcs(arcs, "Arcs", onEachArc));
getData()
调用 jQuery AJAX 方法从服务器获取数据:
function getData(url) {
return $.ajax({
url : url,
type: 'GET',
error : function (xhr,status,error){alert("An error happened while loading a file: "+error+".")}
});
}
最佳答案
正如@Tushar 所说,不要立即调用handleArcs
,将其包装在具有适当签名的匿名函数中并将其传递给done
。
getData('Layers/arcs.json').done(function(data) {
handleArcs(data, arcs, "Arcs", onEachArc);
});
我不太明白
layerVar = new L.geoJson(...
在 handleArcs
中。您不希望此赋值影响您传入的 arcs
变量,是吗?
关于全局变量作为参数:
var aGlobal = "Test_One";
function TryToChangeIt(aParam) {
aParam = "Test_Two";
}
function MyFunction(aParam) {
alert("Before: " + aGlobal);
TryToChangeIt(aGlobal);
alert("After: " + aGlobal);
}
通过调用 TryToChangeIt(aGlobal);
,我们正在传递全局变量的值 - 不是名称。 javascript 引擎创建一个新的临时变量作为参数,并在调用 TryToChangeIt
之前将 aGlobal
的值分配给它。在 TryToChangeIt
中,我们可以更改此临时变量的值,但这不会影响 aGlobal
的值。
关于作为参数传递的 JavaScript typeof 函数返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913630/
我有一个这样的宏: #include #include #define m_test_type(e) \ do {
我知道 JS 中的严格等于评估两件事:平等和相似 Object.is() 是我能找到的最接近的比较来收集更多的洞察力,它在我的调查中没有提供进一步的帮助。 谁能更好地理解 JS 的内部结构?数组是一个
我正在学习 JavaScript,我在代码中看到使用 typeof 和 typeof() 是一样的,例如: 两种情况下的结果都是数字: console.log(typeof 1); console.l
据我所知,检查 undefined variable 的首选方法是 typeof a === 'undefined'。 但为什么它比 typeof a == 'undefined' 更好?它会在哪些地
今天我在 Linux 内核中遇到了这个宏 (include/linux/kernel.h) #define DIV_ROUND_CLOSEST(x, divisor)( \ {
我收到错误 Type 'typeof Questions' is not assignable to type 'typeof Model'.同时在模型中添加了关系。 问题.model.ts impo
只是四处看看我通常掩盖的地方,然后注意到了这一点。 typeof('apple'); //"string" typeof 'apple'; //"string" 好的,首先,第二个示例是如何工作的?我
我正在尝试检查以下内容 typeof( ICollection<> ).GetTypeInfo().IsAssignableFrom( targetProperty.PropertyType.GetT
所以我明白 typeof 可以是一个运算符还是一个函数。但是当我这样做时 console.log(typeof(typeof)); 我收到了这条消息 未捕获的语法错误:意外的标记 ')' 那么我在这里
我正在使用 node/typescript 为 Discord 开发一个机器人。当我在我的源代码上运行 typescript 编译器时,我收到了这个错误: node_modules/@types/re
我有typeof(List)作为类型对象,但我需要 typeof(List<>)我可以从中使用 MakeGenericType()检索 List 的类型对象,是否可能? 更新:伙计们,谢谢。这似乎是一
我是js的新手,正在努力学习js,你们能告诉我为什么typeof typeof x返回string,提供下面的代码片段,如果我理解这个简单的概念,它将对我有更多帮助: var x=null; cons
简短的例子: #include #include using namespace boost; template BOOST_TYPEOF_TPL(T() + U()) add2(const T&
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
检查 typeof 运算符结果的两个版本之间有什么区别(编译器/解释器/juju wise 等)吗? 我问是因为我多次看到第一个版本,好像它遵循了一个概念,而第二个版本更具可读性并且更好地描述了我的意
这一定很简单,但我找不到解决方案。我有一个我想现在 undefined object 。如何将 typeof 更改为未定义?谢谢! 凯尔 最佳答案 你不能让对象本身undefined,但是你可以让引用
这两种说法有什么区别? if (typeof errorMessage !== undefined) {} 和 if (typeof (errorMessage) !== undefined) {}
有一个简单的问题,我想我知道答案但我不想听来自其他一些人。 假设我将创建一个通用函数: public string GetTypeName() { } 它应该返回类型 T 的名称,这很简单: retu
正如标题所说的那样,typeof (Array, null) 返回 object 而 typeof(null, Array) 返回 函数。 它返回第二个参数的类型。 为什么? 最佳答案 因为 type
这是一个令人尴尬的问题,但我觉得我在过去几个小时里尝试了一切。 我只想在我的属性中添加以下属性 #using #using ... using namespace System::Draw
我是一名优秀的程序员,十分优秀!