简体   繁体   English

如何在参数化函数中使用对象属性?

[英]How to use object properties inside a parameterized function?

I have been working on a dialog in a project and I made it into an object constructor.我一直在一个项目中处理一个对话框,我把它变成了一个对象构造函数。

 function createDialog(title, noClicked = function(){}, yesClicked = function(){}) { this.dialog = document.getElementsByClassName("dialog")[0]; this.dialogTitle = this.dialog.getElementsByTagName("h1")[0]; this.No = this.dialog.getElementsByTagName("button")[0]; this.Yes = this.dialog.getElementsByTagName("button")[1]; var dialog = document.getElementsByClassName("dialog")[0]; this.dialogTitle.innerHTML = title; document.getElementsByClassName("dialogCon")[0].style.display = "flex"; noClicked(); yesClicked(); }
 <div class="dialogCon"> <div class="dialog"> <h1></h1> <button type="button">No</button> <button type="button">Yes</button> </div> </div>

The problem is that when I want to access "this.no" or "this.yes" I keep getting Cannot read property 'No' of undefined .问题是,当我想访问 "this.no" 或 "this.yes" 时,我不断收到无法读取 undefined 的属性 'No' This happened when I used the below code:当我使用以下代码时发生了这种情况:

var d = new createDialog("Sample dialog. Exit?", function() {
  console.log(d.No);
}, function() {
  console.log(d.Yes);
});

I need to use d.No to close the dialog. 我需要使用 d.No 来关闭对话框。 Is there any other way to do it? 有没有其他方法可以做到? Or a fix at least. 或者至少修复。
I am aware of the fact that I could close the dialog from within the constructor itself, but I want to make it possible to do other stuff as well (like detecting whether user choose yes or no). 我知道我可以从构造函数本身内部关闭对话框,但我想让它也可以做其他事情(比如检测用户是否选择是或否)。

Thanks in advance 提前致谢

As you have the constructor now, the callbacks are called immediately, and only after they have been called will the constructor return.由于您现在拥有构造函数,因此会立即调用回调,并且只有在调用它们之后构造函数才会返回。 Two remarks about that:对此有两点说明:

  1. As the constructor has not yet returned at the time that the callbacks are called, the d variable has not yet received a value.由于在调用回调时构造函数尚未返回,因此d变量尚未收到值。 So d in the callback will be undefined .所以回调中的d将是undefined

  2. This is not realistic, as in practice you would only want to call one of the callbacks, and only when the user has clicked a button.这是不现实的,因为在实践中您只想调用其中一个回调,并且仅当用户单击按钮时才调用。 At that time d would be defined.当时d将被定义。

You can still solve it, by passing an explicit reference to the constructed object.您仍然可以通过传递对构造对象的显式引用来解决它。 You could for instance pass it as this object:例如,您可以将其作为this对象传递:

Change:改变:

noClicked();

...to: ...到:

noClicked.call(this);

And then in your callback, change:然后在您的回调中,更改:

console.log(d.No);

...to: ...到:

console.log(this.No);

You are calling noClicked() and yesClicked() immediately inside constructor.您立即在构造函数中调用noClicked()yesClicked() I think you would want to call them on click of No and Yes .我想你会想点击NoYes给他们打电话。 You need to add these functions to event listeners of the buttons.您需要将这些函数添加到按钮的事件侦听器中。 Try below snippet.试试下面的片段。

 function createDialog(title, noClicked = function(){}, yesClicked = function(){}) { this.dialog = document.getElementsByClassName("dialog")[0]; this.dialogTitle = this.dialog.getElementsByTagName("h1")[0]; this.No = this.dialog.getElementsByTagName("button")[0]; this.Yes = this.dialog.getElementsByTagName("button")[1]; var dialog = document.getElementsByClassName("dialog")[0]; this.dialogTitle.innerHTML = title; document.getElementsByClassName("dialogCon")[0].style.display = "flex"; this.No.addEventListener('click',noClicked); this.Yes.addEventListener('click',yesClicked); } var d = new createDialog("Sample dialog. Exit?", function() { console.log(d.No); }, function() { console.log(d.Yes); });
 <div class="dialogCon"> <div class="dialog"> <h1></h1> <button type="button">No</button> <button type="button">Yes</button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM