[英]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);
});
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:
对此有两点说明:
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
。
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 .
我想你会想点击No和Yes给他们打电话。 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.