jQuery each() closure - how to access outside variable

前端 未结 3 1148
时光取名叫无心
时光取名叫无心 2021-02-05 07:44

What\'s the best way to access my this.rules variable from within $.each()? Any explanation of why/how would also be helpful!

app.Style = function(node) {
    th         


        
3条回答
  •  梦如初夏
    2021-02-05 08:16

    The answer above by João Silva is not a good solution as it creates a global variable. You are not actually passing a "self" variable to the each function by reference, but are instead referencing the global "self" object.

    In the example above "this" is the window object and setting "var self = this" isn't really doing anything.

    The Window object has two self-referential properties, window and self. You can use either global variable to refer directly to the Window object.

    In short, both window and self are references to the Window object, which is the global object of client-side javascript.

    Creating a closure function is a better solution.

    Screenshot showing window and self comparison

提交回复
热议问题