返回使用在父函数中声明的var的函数

时间:2018-12-01 13:05:03

标签: javascript function random scope

此函数可以从给定数组中随机选择项目,直到所有项目都被提取,然后重复

我实际上从帖子中复制了代码,因为我不明白变量副本的作用域是如何工作的。

PS:这是帖子How to efficiently randomly select array item without repeats?

提前感谢您的帮助。

2018-12-01T12:43:20.525722Z DEBUG [main] (#JOIN) - Resolving join with 4 stages:
 # Table Identifier                 Join Type    R# Referenced Table Identifier      Field            Operation    Referenced Field
 0 Employees.employees.employees    null         -1 null                             null             null         null            
 1 Employees.employees.dept_emp     INNER_JOIN    0 Employees.employees.employees    emp_no           EQUAL        emp_no          
 2 Employees.employees.departments  INNER_JOIN    1 Employees.employees.dept_emp     dept_no          EQUAL        dept_no         
 3 Employees.employees.salaries     INNER_JOIN    0 Employees.employees.employees    emp_no           EQUAL        emp_no          
2018-12-01T12:43:20.632068Z DEBUG [main] (#JOIN) - JoinSpliteratorBuilderImpl computed the following advancer types:
InnerJoinOneToMany
InnerJoinOneToOne
InnerJoinOneToMany
Exception in thread "main" java.lang.UnsupportedOperationException: Unknown column name 'dept_name'.
    at com.company.employees.employees.employees.employees.generated.GeneratedEmployeesEntityStoreSerializerImpl.isNull(GeneratedEmployeesEntityStoreSerializerImpl.java:205)
    at com.speedment.enterprise.datastore.runtime.internal.function.predicate.EntityIsNullPredicateImpl.asReferencePredicate(EntityIsNullPredicateImpl.java:54)
    at com.speedment.enterprise.datastore.runtime.internal.aggregator.AggregatorAdapterImpl.adaptIsNull(AggregatorAdapterImpl.java:104)
    at com.speedment.enterprise.datastore.runtime.internal.aggregator.TupleAggregatorAdapterImpl.internalAdaptIsNull(TupleAggregatorAdapterImpl.java:137)
    at com.speedment.enterprise.datastore.runtime.internal.aggregator.TupleAggregatorAdapterImpl.adaptIsNull(TupleAggregatorAdapterImpl.java:53)
    at com.speedment.enterprise.aggregator.internal.action.nullable.NullableKeyAction.tupleInitiator(NullableKeyAction.java:95)
    at com.speedment.enterprise.aggregator.internal.AggregationCollectorImpl.lambda$asTupleCollector$6(AggregationCollectorImpl.java:162)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1492)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
    at com.speedment.enterprise.aggregator.internal.AggregationCollectorImpl.asTupleCollector(AggregationCollectorImpl.java:163)
    at com.speedment.enterprise.datastore.runtime.internal.stream.TupleStream.collect(TupleStream.java:546)
    at TestCases.AverageSalaryPerDepartment.executeQuery(AverageSalaryPerDepartment.java:79)
    at TestCases.SpeedmentTestImpl.executeWarmUp(SpeedmentTestImpl.java:45)
    at TestCases.SpeedmentTestImpl.execute(SpeedmentTestImpl.java:23)
    at SpeedmentTestManager.executeTests(SpeedmentTestManager.java:24)
    at Main.main(Main.java:58)

Process finished with exit code 1

1 个答案:

答案 0 :(得分:2)

这是JavaScript [closures]的示例。

  

根据MDN,JavaScript表单闭包中的函数。闭包是函数和在其中声明该函数的词法环境的组合。此环境由创建闭合时在范围内的所有局部变量组成。

在您的情况下,chooser是对该函数实例的引用,该实例在调用函数randomNoRepeats时返回。此返回函数的实例维护对其词法环境的引用,该词法环境中存在变量copy。因此,在调用chooser时,变量copy仍然可以使用。

here中了解有关闭包的更多信息。