[英]How can i use variable inside Vanilla JavaScript class to make a function based on boolean
I have a JS Class which is making a function of giving a border to an element when i click to another element.我有一个 JS Class,当我单击另一个元素时,它正在制作一个 function 为元素提供边框。 That means when I click on .trigger
class element, it will give a border to .content
class element.这意味着当我单击.trigger
class 元素时,它将为.content
class 元素提供边框。 But this function only should be happen based on a Boolean condition.但是这个 function 只应该基于 Boolean 条件发生。
If Boolean is Yes it should be give border, otherwise it should not work.如果 Boolean 是 Yes 它应该给边界,否则它不应该工作。 My code works when I set the method inside the constructor parentheses with this
keyword and I can also declare variable there.当我使用this
关键字在构造函数括号内设置方法时,我的代码可以工作,并且我也可以在那里声明变量。 But I need the method outside the constructor based on my other code.但是我需要基于我的其他代码的构造函数之外的方法。
So how can I possible declare variable outside the constructor and inside the class.那么如何在构造函数之外和 class 内部声明变量。 I need this using Class approach based on my project.我需要根据我的项目使用 Class 方法。
My code is as follows.我的代码如下。
class ParentSelector { constructor(trigger, content, condition) { this.trigger = document.querySelector(trigger); this.content = document.querySelector(content); } let outline = condition; makeOutline() { this.trigger.addEventListener("click", (e) => { if (condition) { e.target.nextElementSibling.style.border = "2px solid red"; } }) } } let a = new ParentSelector(".trigger", ".content", true); a.makeOutline();
<div class="one"> <div class="trigger">Trigger</div> <div class="content">Content</div> </div>
First, why some onClick
whose only use case is the if-condition?首先,为什么有些onClick
的唯一用例是 if 条件? Shouldn't you be like, inside makeOutline()
:你不应该像,在makeOutline()
里面:
makeOutline() {
if (this.condition) {
this.trigger.addEventListener("click", (e) => {
e.target.nextElementSibling.style.border = "2px solid red";
})
}
}
? ?
Second, why are you trying to set local variables outside constructor/methods?其次,你为什么要在构造函数/方法之外设置局部变量?
UPDATE : I see you're asking about declaring a class field outside of its constructor.更新:我看到你在问关于在其构造函数之外声明一个 class 字段。 In that case, you declare it at the top of the class.在这种情况下,您在 class 的顶部声明它。 The following should work:以下应该有效:
class ParentSelector {
condition = false;
constructor(trigger, content, condition) {
this.trigger = document.querySelector(trigger);
this.content = document.querySelector(content);
this.condition = condition;
}
// ...
}
Feel free to ask me any questions you have about this!如果您对此有任何疑问,请随时问我!
at last i able to find the solution myself after a deep thinking.经过深思熟虑,我终于自己找到了解决方案。 Following is the solution.以下是解决方案。 Following is the code.以下是代码。
class ParentSelector { constructor(trigger, content, condition) { this.trigger = document.querySelector(trigger); this.content = document.querySelector(content); this.outline = condition; } makeOutline(){ this.trigger.addEventListener("click", (e) => { if (this.outline) { e.target.nextElementSibling.style.border = "2px solid red"; } }) } } let a = new ParentSelector(".trigger",".content", true); a.makeOutline();
<div class="one"> <div class="trigger">Trigger</div> <div class="content">Content</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.