如何在 JavaScript 中访问当前范围之外的变量?

发布于 2024-08-31 05:26:53 字数 628 浏览 4 评论 0原文

我正在用 javascript 编写一个应用程序,但无法弄清楚如何访问此 jquery 解析中函数中声明的变量。在内部我可以访问全局变量,但我真的不想为这些值创建全局变量。

基本上我想从 simulationFiles 变量中的 xml 文档中提取文件名。我检查节点属性是否与 simName 相等,并提取 xml 元素内的两个字符串,我认为这部分工作正常。

如何提取这些 xml 元素并将它们附加到局部变量?

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           initFileName += $(this).find("init").text();
           eventsFileName += $(this).find("events").text();
       }
    });
}   

I'm writing an application in javascript and cannot figure it out how to access the variables declared in my function, inside this jquery parse. Inside I can access global variables, but I don't really want to create global vars for these values.

Basically I want to extract file names from an xml document in the simulationFiles variable. I check if the node attribute is equal with the simName and extract the two strings inside the xml elements, that part I think it's working.

How can I extract those xml elements and append them to local variables?

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           initFileName += $(this).find("init").text();
           eventsFileName += $(this).find("events").text();
       }
    });
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀梦 2024-09-07 05:26:53

CsvReader 函数中的 thiseach() 回调中的 this 不同(相反,它是迭代中的当前元素)。要访问回调中外部函数的作用域,我们需要能够通过另一个名称来引用它,您可以在外部作用域中定义该名称:

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    var self = this; // reference to this in current scope
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           // access the variables using self instead of this
           self.initFileName += $(this).find("init").text();
           self.eventsFileName += $(this).find("events").text();
       }
    });
}

The this in the CsvReader function is not the same this in the each() callback (where instead it is the current element in the iteration). To access the scope of the outer function within the callback, we need to be able to reference it by another name, which you can define in the outer scope:

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    var self = this; // reference to this in current scope
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           // access the variables using self instead of this
           self.initFileName += $(this).find("init").text();
           self.eventsFileName += $(this).find("events").text();
       }
    });
}
生死何惧 2024-09-07 05:26:53

我制作了一个 工作演示 (我将其更改为使用类,以便它可以与 HTML 一起使用)。

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    var context = this;
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           context.initFileName += $(this).find("init").text();
           context.eventsFileName += $(this).find("events").text();
       }
    });
}   

I made a working demo (I changed it to use classes so it would work with HTML).

function CsvReader(simName) {
    this.initFileName = "somepath";
    this.eventsFileName = "somepath";
    var context = this;
    $(simulationFiles).find('simulation').each(function() {
       if ($(this).attr("name") == simName) {
           context.initFileName += $(this).find("init").text();
           context.eventsFileName += $(this).find("events").text();
       }
    });
}   
北笙凉宸 2024-09-07 05:26:53

要使其工作,您可以做的最简单的更改是...将每个函数中的函数从正常( function() {} )更改为箭头函数( () => {} ),它将自动获取函数的上下文它是被定义的。

The simplest change you can do to make it work is... Change your function in each from normal ( function() {}) to arrow function ( () => {} ) that will automatically take the context of the function in which it is defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文