简体   繁体   English

有人可以解释此代码示例中this.value的含义吗?

[英]Can someone explain what this.value means in this example of code?

I'm new to JavaScript and am still learning, I recently wrote this code and it works but I can't understand what this.value refers to in this instance: 我刚接触JavaScript并且仍在学习中,我最近编写了这段代码并且可以正常工作,但是我不明白th​​is.value在这种情况下指的是什么:

function displayMatches() {
 const matchArray = findMatches(this.value, cities); //what does this.value refer to?

 const html = matchArray.map(place => {

 const regex = new RegExp(this.value,'gi');

 const cityName = place.city.replace(regex,`<span class="hl">${this.value}
 </span>`);

 const stateName = place.state.replace(regex,`<span class="hl">${this.value}
 </span>`);

return `
<li>
  <span class="name">${place.city}, ${stateName}</span>
  <span class="population">${numberWithCommas(place.population)}</span>
</li>
`;
}).join ('');
suggestions.innerHtml = html;
}

I thought it was referring in wordsToMatch, here is all of the code: 我认为这是在wordsToMatch中引用的,这是所有代码:

const cities = [];
fetch(endpoint)
  .then(blob => blob.json())
  .then(data => cities.push(...data));

function findMatches(wordToMatch) {
  return cities.filter(place => {
    const regex = new RegExp(wordToMatch, 'gi');
    return place.city.match(regex) || place.state.match(regex)
  });
}

function displayMatches() {
  const matchArray = findMatches(this.value, cities);
  const html = matchArray.map(place => {
   const regex = new RegExp(this.value, 'gi');
    const cityName = place.city.replace(regex, `<span class="hl">${this.value}</span>`);
    const stateName = place.state.replace(regex, `<span class="hl">${this.value}</span>`);
return `
  <li>
    <span class="name">${cityName}, ${stateName}</span>
    <span class="population">${numberWithCommas(place.population)}</span>
  </li>
`;
  }).join('');
  suggestions.innerHTML = html;
}

const searchInput = document.querySelector('.search');
const suggestions = document.querySelector('.suggestions');

searchInput.addEventListener('change', displayMatches);
searchInput.addEventListener('keyup', displayMatches);

Can anyone help explain this to my so I understand? 任何人都可以帮我解释一下,以便我理解吗? Would really appreciate it! 非常感谢! I find the whole .this() method still very confusing. 我发现整个.this()方法仍然很混乱。

when you call some method binding some event this is the information what that event send to handler method. 当您调用绑定某个事件的某个方法时, 就是该事件发送给处理程序方法的信息。 In this line of your code: 在这段代码中:

searchInput.addEventListener('change', displayMatches);

You are listening the element searchInput then, in displayMatches() context this is the searchInput element. 您正在监听元素searchInput,然后在displayMatches()上下文中, 是searchInput元素。

That way this.value is searchInput.value. 这样,this.value是searchInput.value。

Have you got it? 你明白了吗?

Inside a function, the value of this depends on how the function is called. 在函数内部,此值取决于函数的调用方式。 It refers to the DOM element which called the function. 它是指调用该函数的DOM元素。
Let's say this is your JavaScript : 假设这是您的JavaScript:

function alertMessage () {
    alert(this.value);
}

And this is your HTML : 这是您的HTML:

<input type="text" onmousehover="alertMessage()" value="Test">

When the mouse will hover the input element JavaScript will alert "Test", because the value of the input with which you called the function is Test . 当鼠标悬停在输入元素上时,JavaScript将警告“ Test”,因为调用该函数的输入值为Test

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

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