javascript - 如何从嵌套函数调用中传递变量

标签 javascript jquery jsplumb

我正在执行嵌套函数调用,但同时我需要将变量传递给嵌套函数,我可以这样做吗?这就是我正在尝试做的事情

allSourceEndpoints.push(jsPlumb.addEndpoint(toId, sourceEndpoint(index), { anchor:sourceAnchors[i], uuid:sourceUUID }));


sourceEndpoint(index) = {
            endpoint:"Dot",

            paintStyle:{ fillStyle:"#225588",radius:3 },
            isSource:true,
            isTarget:true,
            maxConnections:-1,
        //  connector:[ "Flowchart", { stub:[40, 60], gap:10 } ],
        //  connector:[ "Flowchart"],

            hoverPaintStyle:connectorHoverStyle,
            connectorHoverStyle:connectorHoverStyle,
            dragOptions:{},
            overlays:[
                [ "Label", { 
                    location:[0.5, 1.5], 
                    label:""+startEnd[index].start,
                    cssClass:"endpointSourceLabel",
                } ]
            ]
        }

上面的代码不起作用,因为

       index 

传递我正在做的事情。我需要它,因为我需要找出开始。如果我删除该索引引用和行

         label:""+startEnd[index].start,

它工作得很好,但我真的需要包括它。有办法做到这一点吗?

非常感谢您的帮助!

最佳答案

将 sourceEndPoint 构造更改为函数并返回 JSON 对象作为返回值。 即:

sourceEndpoint = function(index) {

    return {
            endpoint:"Dot",

            paintStyle:{ fillStyle:"#225588",radius:3 },
            isSource:true,
            isTarget:true,
            maxConnections:-1,
        //  connector:[ "Flowchart", { stub:[40, 60], gap:10 } ],
        //  connector:[ "Flowchart"],

            hoverPaintStyle:connectorHoverStyle,
            connectorHoverStyle:connectorHoverStyle,
            dragOptions:{},
            overlays:[
                [ "Label", { 
                    location:[0.5, 1.5], 
                    label:""+startEnd[index].start,
                    cssClass:"endpointSourceLabel",
                } ]
            ]
        };
    }

关于javascript - 如何从嵌套函数调用中传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13131088/

相关文章:

javascript - 如何使jss或幻灯片的出现成为可触发事件?

javascript - 如何使用jquery将变量附加到div?

javascript - jsPlumb:迭代所有连接并在控制台中列出从 到 的列表

javascript - 使用按钮创建新元素并包含内容 'Table' |查询器

javascript - 如何创建一个具有多种状态的按钮?

jquery - iPhone/iPad 上的点击监听器

javascript - JSPlumb 图到 JSON

php - 以特定时间间隔从 jquery 调用 php 文件

javascript - 从javascript中同一div中的下拉列表中选择类别后显示项目

angularjs - 使用 AngularJS 和 jsPlumb(在 AngularJS Controller 中使用 jsPlumb 函数)