简体   繁体   English

从外部范围调用私有/嵌套javascript函数

[英]Calling a private/nested javascript function from an outer scope

I have my javascript code like this . 我有这样的JavaScript代码。 Inside that I have an init() function and in that function I have an options JSON object and in that object I have a function defined as objectselected(). 在其中,我有一个init()函数,在该函数中,我有一个选项JSON对象,在该对象中,我有一个定义为objectselected()的函数。 How I call that function in a button click event 我如何在按钮单击事件中调用该函数

I have tried like this WorkFlow.init().options.Objectselected() but it is not working, 我已经尝试过像这样的WorkFlow.init()。options.Objectselected(),但是它不起作用,

var WorkFlow = {
connectionData: [],
    selectedTouchpoints: [],
    init: function () {
        var options = {
            palleteId: "myPaletteElement",
            elementId: "playAreaContainer",
            TextStoreList: ['One', 'Two', 'Three'],
            LinkTextStoreList: $('#drpLinkType option').map(function () {
                return this.text;
            }).get(),
            shapeList: ['RoundedRectangle', 'Circle', 'Rectangle', 'Ellipse', 'Square', 'Diamond', 'Card', 'Database'],
            diagramUpdate: function (e) {

            },
            objectSelected: function (e) {
            },

            linkUpdate: function (e) {

            },
            initialize: function () {

                }



        myGraph = new Graph(options);
        options.initialize();
    },
}

How to call that function. 如何调用该函数。

You need to return options as it is inside init function's scope 您需要返回options因为它位于init函数的作用域内

var WorkFlow = {
    connectionData: [],
        selectedTouchpoints: [],
        init: function () {
            var options = {
                palleteId: "myPaletteElement",
                elementId: "playAreaContainer",
                TextStoreList: ['One', 'Two', 'Three'],
                LinkTextStoreList: $('#drpLinkType option').map(function () {
                    return this.text;
                }).get(),
                shapeList: ['RoundedRectangle', 'Circle', 'Rectangle', 'Ellipse', 'Square', 'Diamond', 'Card', 'Database'],
                diagramUpdate: function (e) {

                },
                objectSelected: function (e) {
                },

                linkUpdate: function (e) {

                },
                initialize: function () {

                }

            myGraph = new Graph(options);
            options.initialize();
            return options;
        },
    }

And call it as WorkFlow.init().objectSelected(); 并将其称为WorkFlow.init().objectSelected();

One way around is you can return options and than call it. 一种解决方法是您可以返回选项,然后调用它。

init: function () {
        var options = {
            ...your code..}
        return options;
    },

and call it than 并称之为

var options = WorkFlow.init();
options.Objectselected();

Building on Patrick's comment, you'd need to return options from the init function: 基于Patrick的评论,您需要从init函数返回options

var WorkFlow = {
connectionData: [],
    selectedTouchpoints: [],
    init: function () {
        var options = {
            palleteId: "myPaletteElement",

            ...    

        options.initialize();
        return options;            
    },
}

As it stands, you have no access to options because it's a local variable - that is, local to its scope. 就目前而言,您无权访问options因为它是一个局部变量-也就是其作用域的局部变量。

To access its contents, you'll need to return it from init() . 要访问其内容,您需要从init()返回它。

Think about it: 考虑一下:

WorkFlow.init()

Currently this returns undefined , because your init() returns nothing. 当前,这返回undefined ,因为您的init()返回任何内容。 You're trying to chain like in jQuery, but that relies on the API always returning the instance. 您试图像在jQuery中那样进行链接,但这依赖于API始终返回实例。 Your path finds a dead-end at init() . 您的路径在init()处发现了死角。

To fix this, have init() return options - or at least the part of it you want to access from outside - an "export". 要解决此问题,请使用init()返回options -或至少要从外部访问的部分-“导出”。

So (basic example) 因此(基本示例)

init: function() {
    var options {
        my_func: function() { }, //<-- we want outside access to this
        private: 'blah' //<-- this can stay private - leave it out of the export
    }
    //return an export, exposing only what we need to
    return {
        my_func: options.my_func
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript从嵌套函数中调用外部函数 - Javascript calling outer function from within nested function 如何在Javascript中从外部范围调用函数? - How to call a function from outer scope in Javascript? 从javascript的闭包内部调用外部函数 - Calling a outer function from within a closure in javascript Javascript中的作用域链,并在全局作用域中调用嵌套函数 - Scope chain in Javascript and calling nested function within the global scope Javascript范围:将外部函数中的变量传递给内部函数 - Javascript scope: passing a variable from an outer function to an inner function 将javascript参数从外部范围传递到匿名回调函数 - Passing a javascript parameter from an outer scope to an anonymous callback function javascript 从外部 scope 变量访问成员 function - javascript accessing a member function from outer scope variable 从Javascript调用AngularJS隔离范围函数 - Calling an AngularJS isolated scope function from Javascript Javascript函数范围和onclick调用 - Javascript function scope and calling from onclick Javascript:使用&#39;this&#39;访问&#39;private&#39;子函数中的外部范围变量时得到&#39;undefined&#39; - Javascript: got 'undefined' when using 'this' to access outer scope variable in 'private' child function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM