标签: nested-function

未捕获的TypeError:数字不是函数

我只是想了解这段代码发生了什么,我深入研究了嵌套函数和更高阶的主题,以及return语句.但我仍然希望结束这种怀疑(不确定连续两个返回声明)这导致我在这里,需要帮助以便我的理解,以及所有适用的.

function higher_order(){
  return function addNum()
  {  
    return 1+1;
    return function subNum()
    {
      return 1-1;
    };
  };
}; 
alert(higher_order()()());
Run Code Online (Sandbox Code Playgroud)

javascript return typeerror nested-function

1
推荐指数
1
解决办法
6569
查看次数

执行捕获变量的嵌套函数分配

scala 中的嵌套函数可以捕获父函数中的变量。

例如

def outer = {
    var a = 0
    def inner = {
        a = 42
    }
    inner()
    a
}
Run Code Online (Sandbox Code Playgroud)

在 C# 中,这是通过将所有捕获的变量存储在一个结构体上并在 byref 中传递该结构体来实现的。这避免了嵌套函数分配,除非您将其转换为函数对象。在sharplab中看到这个例子

然而,在 scala 中,您不能通过 ref 传递变量,因此唯一可行的方法是将所有捕获的变量存储在一个对象上,然后传入该对象。

这是否意味着嵌套函数的每次调用都会在它捕获 scala 中的任何变量时进行分配?

closures scala nested-function

1
推荐指数
1
解决办法
169
查看次数

错误:此处不允许函数定义。如何纠正这个问题?

这是代码:

#include<stdio.h>

int main(){

// prints I stars

void printIStars(int i) {

  // Count (call it j) from 1 to i (inclusive)

  for (int j = 1; j <= i; j++) {

    // Print a star

    printf("*");

  }

}


// prints a triangle of n stars

void printStarTriangle(int n) {

  // Count (call it i) from 1 to n (inclusive)

  for (int i = 1; i <= n; i++) {

    // Print I stars

    printIStars (i);

    // Print a newline …
Run Code Online (Sandbox Code Playgroud)

c nested function nested-function function-definition

1
推荐指数
1
解决办法
2187
查看次数

Python的?...动态调用嵌套函数

这是pythonic实现吗?

我正在使用包装器从字符串参数动态调用嵌套函数,以减少调用不存在的函数的机会.这是一个例子,我想对arg1和arg2进行不同的比较(根据==,> =,<等等)...

class ComparisonClass(object):
    def__init__(self):
        pass

    def comparison(self,arg1,arg2,comparison):
        def equal_to():               
            pass
        def greater_than():
            pass
        def less_than():
            pass

        return locals()[comparison]()

    def comparison_equal_to(self,arg1,arg2):
        return self.comparison(arg1,arg2,'equal_to')

    def comparison_greater_than(self,arg1,arg2):
        return self.comparison(arg1,arg2,'greater_than')

    def comparison_less_than(self,arg1,arg2):
        return self.comparison(arg1,arg2,'less_than')
Run Code Online (Sandbox Code Playgroud)

python locals nested-function

0
推荐指数
1
解决办法
213
查看次数

在Haskell中调用嵌套函数

我已经定义了一个名为函数initials内部的函数person,但我无法弄清楚如何在initials外面调用person:

main =
    --I attempted to print the output of the initials function here.
    (putStrLn ((person "firstName" "lastName") . initials)) --Not in scope: `initials'
    --If this function call worked correctly, the output would be "f.l.".

person firstName lastName =
    firstName ++ ["."] ++ lastName
    where
        fullName = firstName ++ " " ++ lastName
        firstInitial = firstName !! 0
        lastInitial = lastName !! 0
        initials = [firstInitial] ++ "." ++ [lastInitial] ++ "." …
Run Code Online (Sandbox Code Playgroud)

haskell nested-function

0
推荐指数
1
解决办法
1068
查看次数

为什么我无法读取嵌套函数中全局变量的变化?

//这是关于嵌套函数和计时器函数的代码问题

    var num = 0; //the key of this problem
	var timer = null;
	timer = setInterval(function() {
        //num could change from 0 to 9 in this function
		console.log(num); 
		setTimeout(function() {
		console.log(num); //but in this place, num is always 0,why?
		}, 2000);	

		num++;					
		if (num >= 10) {						
			num = 0;
		clearInterval(timer);
		}
	}, 100);
Run Code Online (Sandbox Code Playgroud)

javascript nested-function

0
推荐指数
1
解决办法
41
查看次数

JavaScript使用构造函数设置变量

我试图在构造函数中设置一个可以由嵌套函数表达式调用的变量.不太确定如何做到这一点

var test = function() {
  var a;

  function test(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;

  }
  test.getvariableA = function() {
    //not returning a variable that is supposed to be set by the constructor
    console.log(this.a);
  };
  return test;
}();

var t = new test("pizza", "pasta", "steak");
//does not return the variable
test.getvariableA();
//this returns the variable
console.log(t.a);
Run Code Online (Sandbox Code Playgroud)

test.getvariableA();

这应该返回构造函数设置的变量.也许我对另一种语言感到困惑,谢谢你提前帮忙.

javascript variables nested-function

0
推荐指数
1
解决办法
464
查看次数

PHP 中嵌套函数的怪异

可能的重复:
当多次调用外部函数时,php 中的嵌套函数会引发异常

为什么

function foo(){
    function bar(){
    }    
}
bar(); 
Run Code Online (Sandbox Code Playgroud)

在不存在的函数栏上返回致命错误

尽管

function foo(){
    function bar(){
    }    
}
foo(); 
foo(); 
Run Code Online (Sandbox Code Playgroud)

bar() 的重复声明出现致命错误?

php 将该函数作为全局函数还是在父函数作用域中处理?

php function nested-function

-1
推荐指数
1
解决办法
139
查看次数

计算向量 R 中值之间的距离

我有以下多重集 X,我想在其中找到所有数字之间的距离。有什么方法可以将其集成到 FOR 循环中,这样如果给我一个不同大小的多重集,我就不必像下面那样手动执行此操作?

对于此示例,最终答案是 [0,2, 2, 3, 3, 4, 5, 6, 7, 8, 10] (已排序)

X=c(0,10,8,3,6)
L=length(X)
print(L)

##for(i in seq(from=1, to=L )){}

print(abs(X[1]-X[2]),  abs(X[1]-X[3]),
    abs(X[1]-X[4]),  abs(X[1]-X[5]),
    abs(X[1]-X[6]),  

    abs(X[2]-X[3]), abs(X[2]-X[4]),
    abs(X[2]-X[5]),  abs(X[2]-X[6]),


    abs(X[3]-X[4]),  abs(X[3]-X[5]),
    abs(X[3]-X[6]),  


    abs(X[4]-X[5]), abs(X[4]-X[6]),


    abs(X[5]-X[6])  

        )
Run Code Online (Sandbox Code Playgroud)

loops r vector nested-function rstudio

-1
推荐指数
1
解决办法
3637
查看次数