1

我有一个本地化到主函数的函数,我想用this它来调用它,但它似乎不起作用。

我的代码有:

function option(room,slot){

    var div_id = document.getElementById(room);
    var opacity = window.getComputedStyle(div_id).opacity

    transition_opacity(div_id,opacity,0,function(){this.load});


    function load(){
    console.log('test'); //does not happen      
       }
}

当我使用调用时,我是否误解了使用this或范围丢失?function(){}load

4

5 回答 5

2

this从您的代码来看,可以引用什么对象并不明显。这取决于如何option调用。但是,如果无论如何在load函数内部定义函数option,最好直接引用它。但是,您必须将声明移到调用test上方:transition_opacity

function option(room,slot){
    var div_id = document.getElementById(room);
    var opacity = window.getComputedStyle(div_id).opacity;

   function load() {
       console.log('test');
   }

   transition_opacity(div_id,opacity,0,load);
}

如您所见,我只是load直接引用。您可以创建另一个调用内部函数load函数(即- 注意调用该函数function() { load(); }的括号),但这不会给您带来任何好处,只会将另一个不需要的函数添加到堆栈中。所以只需参考实际功能本身。

有关this关键字的更多信息,请查看此问题。剧透:它比你想象的要复杂。

于 2013-10-17T21:36:20.780 回答
1

在这种情况下,范围this丢失了,可能指向文档。您可以将其捕获到外部范围内的变量中,以使其按预期工作。

var context = this;
transition_opacity(div_id,opacity,0,function(){context.load();})

但是,上述方法不起作用。这是因为负载在 的上下文中不存在this。您需要这样定义加载函数:

context.load = function(){
   console.log('test');
}
于 2013-10-17T21:32:57.817 回答
1

两个都。

首先,您的 load 函数不是任何 this 的成员/属性,就像您对其进行编码的方式一样。您的 load 函数只是存在于您的 option 函数中的嵌套函数,正如在其他响应中隐含地指出的那样。

在你的选项函数中,如果你想让 'load' 成为 'this' 的成员,你需要这样说,像这样:

function option(){
    this.load = function(){};  // now load is actually a property of whatever this is
}

其次,您和其他发帖人是正确的,当您的匿名函数被调用时,“this”不再是相同的“this”。

每当您调用一个函数时,都会创建一个全新的“this”并存在于该函数的范围内。如果你只是调用这样的函数:

transition_opacity(args);

.. 然后在transition_opacity 中,“this”只是指窗口对象,或者可能是window.document。要让“this”引用除 window 或 window.document 以外的任何内容,您需要(实际上)执行以下操作之一:

myObject.transition_opacity(args);

transition_opacity.call(myObject, arg1, arg2, ..);

transition_opacity.apply(myObject, argArray);

或者

var myObject = new transition_opacity(args);

在每一种情况下,在transition_opacity 中,“this”都指代myObject(或者,在最后一种情况下,它指的是正在创建并分配给myObject 的新对象)。

这是一种执行您正在尝试执行的操作的方法:

var MyNamespace = {

    option: function(room,slot){
        var div_id = document.getElementById(room);
        var opacity = window.getComputedStyle(div_id).opacity;

        var _this = this;
        transition_opacity(div_id,opacity,0,function(){
            // Careful! Inside here, 'this' is just window or window.document,
            // unless transition_opacity sets it to something using call or apply, 
            // in which case that 'this' is probably not the 'this' you want.
            // So carefully refer to the saved instance of 'this':
            _this.load();
        });
    },

    load: function(){
        console.log('test'); // now it should happen   
    }
}

.
.
MyNamespace.option(room, slot);  // inside option, 'this' is MyNamespace.

这是另一种方法:

function MyClass(){};
MyClass.prototype = {
   // all the same stuff that is in MyNamespace above..
};
.
.
var myObject = new MyClass();
myObject.option(room, slot);

清如泥?

于 2013-10-17T22:56:31.483 回答
0

您已将另一个函数中的“加载”定义为“函数声明”,因此现在它只能在“选项”函数中以及在此函数中以名称“加载”定义的其他函数中访问。无论“this”是什么,您都无法使用“this.load”访问它。如果你想以 'this.load' 的形式访问 'load' 函数,你可以试试这个例子来了解 'this' 键盘是如何工作的

// Function Declaration
function f1(callback){
  callback();
};
// Function Declaration
function f2(){
  // Function Expression
  this.load = function(){
    console.log("test");
  };
  f1(this.load);
};
var obj = new f2(); // test, this == obj, so obj.load() now exists
obj.load(); //test, this == obj
f2(); //test, this == window, so window.load() now exists
load(); //test, window is the global scope
于 2013-10-17T22:10:50.007 回答
0

只需使用

transition_opacity(div_id,opacity,0,load);
于 2013-10-17T21:33:51.533 回答