我有一个构造函数,它注册一个事件处理程序:
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)
但它表现出同样的问题.
如何访问正确的对象?
如何将上下文传递给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类,每个方法都返回一个Q
promise.我想知道为什么this
未定义method2
和method3
.有没有更正确的方法来编写此代码?
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
?
我刚刚开始尝试类和异步等待。我使用的是 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) 所以我在我的应用程序中使用了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中使用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阵列扔掉.
尝试调用 …
我想从类中链接方法.我有同步方法的问题,但我不知道如何使用异步方法.
例如,这个类:
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我想要链接,而不是地狱回调.
所以我需要一个按钮在它被点击后被禁用,然后在几秒后它应该再次启用.我添加了一个类按钮禁用,因为我想在同一页面上使用它几次.这是我试过的
$(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 ×8
ecmascript-6 ×3
this ×3
callback ×2
node.js ×2
angular ×1
async-await ×1
chaining ×1
class ×1
hapijs ×1
jquery ×1
php ×1
promise ×1
q ×1
scope ×1
settimeout ×1
typescript ×1