相关疑难解决方法(0)

如何在回调中访问正确的`this`?

我有一个构造函数,它注册一个事件处理程序:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', function () {
        alert(this.data);
    });
}

// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};

// called as
var obj = new MyConstructor('foo', transport);
Run Code Online (Sandbox Code Playgroud)

但是,我无法data在回调中访问已创建对象的属性.它看起来this并不是指创建的对象,而是指另一个对象.

我还尝试使用对象方法而不是匿名函数:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
    alert(this.name);
};
Run Code Online (Sandbox Code Playgroud)

但它表现出同样的问题.

如何访问正确的对象?

javascript callback this

1309
推荐指数
12
解决办法
36万
查看次数

将正确的"this"上下文传递给setTimeout回调?

如何将上下文传递给setTimeout?我想打电话this.tip.destroy(),如果this.options.destroyOnHide在1000毫秒.我怎样才能做到这一点?

if (this.options.destroyOnHide) {
     setTimeout(function() { this.tip.destroy() }, 1000);
} 
Run Code Online (Sandbox Code Playgroud)

当我尝试以上时,this指的是窗口.

javascript callback this settimeout

226
推荐指数
4
解决办法
17万
查看次数

使用promises时,为什么'this'在类方法中未定义?

我有一个javascript类,每个方法都返回一个Qpromise.我想知道为什么this未定义method2method3.有没有更正确的方法来编写此代码?

function MyClass(opts){
  this.options = opts;

  return this.method1()
    .then(this.method2)
    .then(this.method3);
}

MyClass.prototype.method1 = function(){
  // ...q stuff...

  console.log(this.options); // logs "opts" object

  return deferred.promise;
};

MyClass.prototype.method2 = function(method1resolve){
  // ...q stuff...

  console.log(this); // logs undefined

  return deferred.promise;
};

MyClass.prototype.method3 = function(method2resolve){
  // ...q stuff...

  console.log(this); // logs undefined

  return deferred.promise;
};
Run Code Online (Sandbox Code Playgroud)

我可以通过使用bind:

function MyClass(opts){
  this.options = opts;

  return this.method1()
    .then(this.method2.bind(this))
    .then(this.method3.bind(this));
}
Run Code Online (Sandbox Code Playgroud)

但不完全确定为什么bind有必要; 正在.then()杀戮this

javascript this node.js promise q

89
推荐指数
3
解决办法
6万
查看次数

使用类/异步等待时获得未定义的“this”

我刚刚开始尝试类和异步等待。我使用的是 Node 版本 8.9.0 (LTS)。当 I 时console.log(this),我得到undefined而不是对对象的引用。

子处理程序.js

class Handler {
  constructor(props) {
    this.defaultRes = {
      data: successMessage,
      statusCode: 200
    };
  }

  async respond(handler, reply, response = this.defaultRes) {
    console.log(this); // why is `this` undefined????
    try {
      await handler;
      return reply(response.data).code(response.statusCode)
    } catch(error) {
      return reply(error);
    }
  }
}

class SubHandler extends Handler {
  constructor(props) {
    super(props);
    this.something = 'else';
  }

  makeRequest(request, reply) {
    console.log(this); // why is `this` undefined!!
    // in this case, doSomeAsyncRequest is …
Run Code Online (Sandbox Code Playgroud)

javascript async-await ecmascript-6 hapijs ecmascript-2017

7
推荐指数
1
解决办法
4112
查看次数

'this'在function()中未定义.角2

所以我在我的应用程序中使用了uikit确认模式.我的问题是,当我要点击<button>确认时.在this内部功能undefined.这是我的代码......

declare var UIkit:any;

deleteData(dataArr): void {

    UIkit.modal.confirm('Are you sure you want to delete this?', function() {
      console.log(dataArr);
      console.log(this);
      //use service here...
      UIkit.modal.alert('Confirmed!');  
    });
}
Run Code Online (Sandbox Code Playgroud)

在该函数内部我希望使用http请求服务,但我遇到了问题this.我正在使用Angular 2.x.

javascript typescript ecmascript-6 angular

3
推荐指数
1
解决办法
6075
查看次数

在javascript中实现支持对象方法作为映射函数的映射?

我最近尝试在javascript中使用map的实现来创建一堆项目,然后将它们应用于对象添加方法.

首先是沼泽标准的地图实现.

var map = function (fn, a)
{
    for (i = 0; i < a.length; i++)
    {
        a[i] = fn(a[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

建立.

var translateMenu = new Menu;

var languages = [ ['Chinese'   , 'zh-CN']
                , ['German'    , 'de']
                , ['French'    , 'fr']
                , ['Portugese' , 'pt']
                , ['Hindi'     , 'hi']
                ];
Run Code Online (Sandbox Code Playgroud)

我的功能......(不是匿名的,因为它在将translateMenu添加到mainMenu时会被使用.)

var langItem = function (language, subMenu) 
    { 
       return new MenuItem(language[0], 'http://translate.google.com/translate?u=www.example.com&hl=en&ie=UTF-8&tl=en&sl=' + language[1] , "" , subMenu); 

    }

map ( langItem , languages );
Run Code Online (Sandbox Code Playgroud)

这一切都运行良好,我现在有一个MenuItems阵列扔掉.

尝试调用 …

javascript php scope functional-programming

2
推荐指数
1
解决办法
3348
查看次数

如何在javascript ES6类中链接异步方法

我想从类中链接方法.我有同步方法的问题,但我不知道如何使用异步方法.

例如,这个类:

class Example {

  constructor() {
    this.val = 0
  }

  async () {
    setTimeout(() => {
      this.val += 1
      return this
    }, 5000)
  }

  sync () {
    this.val += 1
    return this
  }

  check () {
    console.log('checker', this.val)
    return this
  }

}
Run Code Online (Sandbox Code Playgroud)

这有效:

new Example().sync().check()
> 1
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

new Example().async().check()
> TypeError: Cannot read property 'check' of undefined
Run Code Online (Sandbox Code Playgroud)

PS我想要链接,而不是地狱回调.

javascript class chaining node.js ecmascript-6

2
推荐指数
2
解决办法
9072
查看次数

在jquery中禁用然后启用按钮

所以我需要一个按钮在它被点击后被禁用,然后在几秒后它应该再次启用.我添加了一个类按钮禁用,因为我想在同一页面上使用它几次.这是我试过的

$(document).on('click', '.button-disabled', function (){
                $(this).prop('disabled', true);
                setTimeout(function() {
                $(this).prop('disabled', false);
                }, 3500);
                }); 


<button type="button" id="buttonSettings" class="btn btn-success button-disabled">Sacuvaj</button>
Run Code Online (Sandbox Code Playgroud)

我在网站上查找了类似的问题,但这些都没有帮助,因为每次按钮都会在点击后被禁用,但它永远不会再次启用.任何帮助,将不胜感激.

javascript jquery

0
推荐指数
1
解决办法
1129
查看次数