[英]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.