我正在尝试构建JS库,并且希望将代码作为函数的参数传递。我该怎么办?
我尝试了多种其他方式传递参数,但这不起作用。
调用函数时,这就是我想要的。
var input = new oTinput({
element: '#otinput',
onFileChange: function(file){
console.log('File name is: '+file.name);
$("#rlly").attr("src", URL.createObjectURL(file));
document.getElementById("rllly").load();
console.log("Audio is loaded!");
/*var audiofile = document.getElementsByTagName('audio')[0];
audiofile.src = file;*/
//function handleFiles(event) {
//var files = event.target.files;
/*$("#rlly").attr("src", URL.createObjectURL(file[0]));
document.getElementById("rllly").load();
console.log("Audio is loaded!");*/
//}
//document.getElementById("rll").addEventListener("change", handleFiles, false);
},
请注意,这正是我想要的。这个JS库是oTinput。
答案 0 :(得分:0)
您不能直接传递代码,因为代码块({}
)立即运行,并且什么都不“返回” ...(语法上也是无效的)。
要保留一些代码供以后使用,这就是函数的作用。
因此,只需创建一个新函数并将其传递!
()=>{/* some code */}
将创建一个箭头功能(有关here的更多信息):
var input = new oTinput(() => {
element: '#otinput',
onFileChange: function(file){
console.log('File name is: '+file.name);
$("#rlly").attr("src", URL.createObjectURL(file));
document.getElementById("rllly").load();
console.log("Audio is loaded!");
/*var audiofile = document.getElementsByTagName('audio')[0];
audiofile.src = file;*/
//function handleFiles(event) {
//var files = event.target.files;
/*$("#rlly").attr("src", URL.createObjectURL(file[0]));
document.getElementById("rllly").load();
console.log("Audio is loaded!");*/
//}
//document.getElementById("rll").addEventListener("change", handleFiles, false);
})