var test = function (a) { this.a = a; return function(b){return this.a + b;} }(function (a, b) {return a;} (11,45)); var result = test(7); console.log(result);
时间: 2023-02-11 22:07:23 浏览: 65
这段代码会返回NaN。
它首先定义了一个名为test的函数,该函数接受一个参数a,并返回一个匿名函数,这个匿名函数接受一个参数b并返回this.a * b.
然后这个函数被立即执行了, 传递了一个匿名函数, 参数是(11,45),这个匿名函数执行并返回11
最后,调用test(7), 返回的是 this.a * b, this.a指的是上面的11,b是7,所以返回11 * 7 =NaN
请注意,这段代码存在语法错误,如果要使用乘法运算符,应该使用*号。
相关问题
function webAudio(options){ //public this.volume = parseInt(options.volume || 100) ; // 音量控制单元,值为Number类型,范围为0-100 this.url = options.url || '' ; // 音频资源url,值类型为String类型 this.autoPlay = !!options.autoPlay; // 是否加载完成自动播放,值类型为Boolean类型 this.loopPlay = !!options.loopPlay; // 是否循环播放,值类型为Boolean类型 //private this.buffer = null; this.context = null; this.sourceAudio = null; this.gainNode = null; this.loadReady = false; //初始化 this.init = function () { window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext; if(!AudioContext){ console.error("您的浏览器不支持HTML5 audio API"); return } this.context = new AudioContext(); this.loadResource(); } //下载音频资源 this.loadResource = function () { var _this = this; var xhr = new XMLHttpRequest(); xhr.open('GET',this.url,true); xhr.responseType = 'arraybuffer'; xhr.onload = function () { _this.context.decodeAudioData(xhr.response,function (buffer) { _this.buffer = buffer; _this.prepareAudio(); this.loadReady = true; }) } xhr.send(); } //是否自动播放 this.prepareAudio = function () { this.autoPlay ? this.startAudio() : ''; } //创建音频 this.createAudio = function () { this.sourceAudio = this.context.createBufferSource();//创建一个音频源 相当于是装音频的容器 this.sourceAudio.buffer = this.buffer;// 告诉音频源 播放哪一段音频 this.gainNode = this.context.createGain();//调节音量 this.sourceAudio.connect(this.gainNode); this.changeVolume();//声音 this.sourceAudio.loop = this.loopPlay;//循环 this.gainNode.connect(this.context.destination);// 连接到输出源 } //重新播放 this.startAudio = function () { this.createAudio(); this.sourceAudio.start(0);//开始播放 } //改变音量 this.changeVolume = function (num) { num = num || 0; this.gainNode.gain.value = (this.volume += num) / 100; } //播放转为暂停 this.pauseAudio = function () { this.gainNode.disconnect(this.context.destination) } //暂停转为播放 this.playAudio = function () { this.gainNode.connect(this.context.destination) } //停止播放 this.stopAudio = function () { this.sourceAudio.stop() } //减小声音 this.decVolume = function () { if(this.volume >= 10){ this.changeVolume(-10); } } //增大声音 this.ascVolume = function () { if(this.volume <= 90){ this.changeVolume(10); } } //静音 this.quietVolume = function () { this.gainNode.gain.value = 0; } //静音恢复 this.recoverVolume = function () { this.changeVolume() } //当前音量 this.getVolume = function () { return (this.gainNode.gain.value).toFixed(2) * 100; } this.init(); return this; } window.test = new webAudio({ volume:100, url:'1.mp3', autoPlay:true, loopPlay:true }); //控制台事件 var pauseEle = document.getElementById("pause"); pauseEle.onclick = function() { if (pauseEle.alt === 'Pause') { test.pauseAudio(); } else { test.playAudio(); } }增加播放暂停图片切换
可以将HTML代码中的播放暂停按钮的img标签的src属性设为一个变量,然后在JavaScript中根据当前状态切换该变量的值,从而实现图片切换。例如,可以在JavaScript中添加一个togglePlayPause函数:
```
function togglePlayPause() {
var playPauseButton = document.getElementById("play-pause-button");
if (playPauseButton.src === "play.png") {
playPauseButton.src = "pause.png";
} else {
playPauseButton.src = "play.png";
}
}
```
并在createAudio函数中调用该函数:
```
this.createAudio = function () {
this.sourceAudio = this.context.createBufferSource();
this.sourceAudio.buffer = this.buffer;
this.gainNode = this.context.createGain();
this.sourceAudio.connect(this.gainNode);
this.changeVolume();
this.sourceAudio.loop = this.loopPlay;
this.gainNode.connect(this.context.destination);
togglePlayPause();
}
```
这样,每次播放或暂停音频时,都会切换播放暂停按钮的图片。需要在HTML代码中给该按钮添加一个id属性为"play-pause-button",并将src属性设为默认的播放按钮图片路径。
number.onblur = function () { var res2 = obj.number.reg.test(this.value) if (!res2) { number.nextElementSibling.innerHTML = '手机号格式不正确' } else { number.nextElementSibling.innerHTML = '√' } return res2 } 怎么在外面拿到里面return的值
可以在外部定义一个变量来存储`onblur`事件处理函数中的返回值,然后在需要使用这个返回值的地方调用该变量即可。例如:
```javascript
let isNumberValid = false; // 定义一个变量来存储返回值
number.onblur = function () {
var res2 = obj.number.reg.test(this.value);
if (!res2) {
number.nextElementSibling.innerHTML = '手机号格式不正确';
} else {
number.nextElementSibling.innerHTML = '√';
}
isNumberValid = res2; // 将返回值存储在变量中
return res2;
}
// 在需要使用返回值的地方调用变量
if (isNumberValid) {
// do something
} else {
// do something else
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<script type="text/javascript"> var httpRequest = new XMLHttpRequest(); httpRequest.open('GET','http://1.14.137.211:8888/tealist',true); httpRequest.send(); httpRequest.onreadystatechange = function(){ if(httpRequest.readyState == 4 && httpRequest.status ==200 ){ var data = httpRequest.responseText; var data = JSON.parse(data); console.log(data); function test1(){ $(".increment").onclick = function(){ let n = $(this).siblings(".itxt").val(); console.log(n); n++; $(this).siblings(".itxt").val(n); } return; } function test2(){ $(".decrement").onclick = function(){ let n = $(this).siblings(".itxt").val(); console.log(n); if (n == 0) { return false; } n--; $(this).siblings(".itxt").val(n); } return; } data.forEach(element => { let html = ${element.name}${element.price}¥ - <input type="text" class="itxt" value="0"> + $("#pics").append(html); //document.querySelector("#pics").innerHTML = html; }); } } </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)