匿名函数导致问题

时间:2015-06-07 01:53:29

标签: javascript anonymous-function

唯一能给我带来问题的是执行匿名函数调用。我甚至打了个电话,看看里面的代码是否有问题;事实并非如此。

这是我写的格式:

(function(){})(); 

我很肯定这是正确和标准的使用,但它一直在抛出这个 错误:

  

未捕获的TypeError :(中间值)(中间值)(...)是   不是函数(匿名函数)

网站运行时可以找到错误HERE

上面的代码摘录与我的程序中的内容没有什么不同

2 个答案:

答案 0 :(得分:8)

给您带来麻烦的代码是

#!/usr/bin/python

import networkx as nx
import matplotlib.pyplot as plt
import itertools

H = nx.DiGraph()

axis_labels = ['p','q','r','s']

D_len_node = {}

#Iterate through axis labels
for i in xrange(0,len(axis_labels)+1):
    #Create edge from empty set
    if i == 0:
        for ax in axis_labels:
            H.add_edge('O',ax)
    else:
        #Create all non-overlapping combinations
        combinations = [c for c in itertools.combinations(axis_labels,i)]
        D_len_node[i] = combinations
    #Create edge from len(i-1) to len(i) #eg. pq >>> pqr, pq >>> pqs
    if i > 1:
        for node in D_len_node[i]:
            for p_node in D_len_node[i-1]:
                #if set.intersection(set(p_node),set(node)): Oops
                if all(p in node for p in p_node) == True: #should be this!
                    H.add_edge(''.join(p_node),''.join(node))

#Show Plot
nx.draw(H,with_labels = True,node_shape = 'o')
plt.show() 

删除评论,我们得到

    ctrl.deleteObject = function(obj){
        var index = ctrl.objects.indexOf(obj);
        if( index > -1 ){
            this.objects.splice(index, 1);
        }
    }
    //}


    // //START GAME
     (function(){
     //ctrl.createObject(new PlayerPaddle(50, 50));
        //ctrl.init();
    })();

ctrl.deleteObject = function(obj){ var index = ctrl.objects.indexOf(obj); if( index > -1 ){ this.objects.splice(index, 1); } } (function(){ })(); 的赋值不以分号结束,下一行的括号看起来像赋值的有效延续,因此Javascript不会为您插入分号。您最终会调用您尝试分配给ctrl.deleteObject的函数,而不是分配和匿名函数调用,然后调用其返回值,这不是函数。

答案 1 :(得分:4)

也许你有像

这样的东西
(function() { return 123; })
(function(){})();

变成

(123)();

但是123不是一个功能。所以它抛出

  

TypeError :(中间值)(...)不是函数

要修复它,请添加一个分号:

(function() { return 123; }); // <-- semi-colon
(function(){})(); // No error

注意函数表达式中需要使用分号,但在函数声明中不需要:

function foo() {} // No semi-colon
(function(){})(); // No error