简体   繁体   English

我如何在 Vanilla JavaScript class 中使用变量来制作基于 Z84E2C64F55AB78BA3ZEA5C2 的 function

[英]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.

相关问题 如何使此javascript函数不使用全局变量? - How can I make this javascript function not use a global variable? Vanilla Javascript - 如何在同一个按钮上使用 mouseup 和 mousedown 来更改和还原 css 类值? - Vanilla Javascript - How can I use mouseup and mousedown on the same button to change and revert a css class value? 如何在javascript中的函数内使用具有类名的变量? - How to use a variable with class name inside a function in javascript? 如何使用JavaScript变量在JavaScript函数中获取ASP.NET ClientID? - How can I use a JavaScript variable to get an ASP.NET ClientID inside a JavaScript function? VSCode:如何使Intellisense与普通的Javascript项目一起工作? - VSCode: How can I make Intellisense work with a vanilla Javascript project? 如何在香草 javascript 中制作我的图像旋转循环? - How can i make my image carousal loop in vanilla javascript? 如何创建一个JavaScript类或函数,使下面的代码工作? 我不能使用诺言等待 - How to make a JavaScript class or function that will make the following code work? I can't use promise and await 如何访问类函数中的类变量? - How can I access a class variable inside a class function? 如何在Reactjs组件中使用香草javascript? - How to use vanilla javascript inside Reactjs component? JavaScript,如何使 fill() 成为变量(可以在自定义函数中使用)? - JavaScript, how do I make fill() a variable (that I can use in a custom function)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM