python Lambda到Javascript的转换

python Lambda到Javascript的转换,javascript,python,lambda,Javascript,Python,Lambda,我正在将以下代码从Python转换为JS lat *= (math.pi / 180.0) lon *= (math.pi / 180.0) n = lambda x: wgs84_a / math.sqrt(1 - wgs84_e2*(math.sin(x)**2)) x = (n(lat) + alt)*math.cos(lat)*math.cos(lon) y = (n(lat) + alt)*math.cos(lat)*math.sin(lon) z = (n(lat) * (1-w

我正在将以下代码从Python转换为JS

lat *= (math.pi / 180.0)
lon *= (math.pi / 180.0)

n = lambda x: wgs84_a / math.sqrt(1 - wgs84_e2*(math.sin(x)**2))

x = (n(lat) + alt)*math.cos(lat)*math.cos(lon)
y = (n(lat) + alt)*math.cos(lat)*math.sin(lon)
z = (n(lat) * (1-wgs84_e2) +alt)*math.sin(lat)

这大部分都没有问题,但我不知道如何将带有lambda函数的n=line转换为JS。

var n=function(x){return wgs84_a/Math.sqrt(1-wgs84_e2*Math.pow(Math.sin(x),2))
这里有一个粗略的翻译:

var n = function(x) {
    return wgs84_a / Math.sqrt(1 - wgs84_e2*Math.pow(Math.sin(x), 2));
};
与Python的
lambda
一样,该表达式将创建函数并将其分配给
n
,但不会调用它。代码稍后将通过
n
调用它。它也是一个“闭包”,因为它将在调用时使用您在其中引用的变量(它具有对变量的持久引用,并在调用时使用它们的值,而不是在创建时)

特点:

  • 使用
    var
    声明
    n

  • 使用
    function
    启动函数表达式

  • 该函数的参数在
    函数
    之后的
    ()
    中给出

  • 使用
    return
    关键字从函数返回值

  • 使用JavaScript的各种
    Math
    函数,包括而不是
    ***
    (JavaScript没有用于此的运算符)

类似上面的函数表达式在逐步执行中到达代码时创建函数(就像任何其他表达式一样)。另一种替代方法是使用函数声明:


这将在执行进入包含函数的范围时创建函数;它发生在该范围内的任何分步代码执行之前。(人们有时称之为“提升”,因为无论它出现在何处,它就像是将声明移动到了范围的顶部——“提升”)。它仍然使用在调用时关闭的变量,而不是在创建时关闭的变量;这只是创建函数的另一种方法。

函数内部是否应该有返回?谢谢大家:)我被{Coffee,Live}脚本给宠坏了:)
function n(x) {
    return wgs84_a / Math.sqrt(1 - wgs84_e2*Math.pow(Math.sin(x), 2));
}