将参数传递给回调函数

时间:2018-04-12 09:59:34

标签: javascript arrow-functions

如果触发eventListener,如何将参数传递给箭头函数?

system.graph('rec').create()
:remote config alias g rec.g

schema.propertyKey("name").Text().create();
schema.propertyKey("name").properties("name").add();

2 个答案:

答案 0 :(得分:1)

让你的fileUpload返回内部功能

const fileUpload = (certificateName) => (evt) => {

  console.log(`Name of Certificate: #{certificateName}`);

  let myFile = evt.target.files[0],
      maxFileSize = evt.target.getAttribute('data-fileSizeMax');

  if(myFile.size <= maxFileSize){
   // etc.
  }

}

您需要调用fileUpload函数并传递certificateName

currentFileInput.addEventListener('change', fileUpload(certificateName), false);

答案 1 :(得分:1)

您需要将函数调用包装在另一个函数中

let certificateName = document.querySelector('.js--c-name').value;

currentFileInput.addEventListener('change', function(e){
  fileUpload(e, certificateName ); //observe changes in this line
}, false);

const fileUpload = (evt, certificateName) => { //pass the arguments 
  //rest of your code as is    
}