addEventListener和之间有什么区别onclick?
var h = document.getElementById("a");
h.onclick = dothing1;
h.addEventListener("click", dothing2);
Run Code Online (Sandbox Code Playgroud)
上面的代码一起存在于一个单独的.js文件中,它们都完美地工作.
我注意到调用带有空括号的函数或根本没有任何括号时的区别.但是,我没有向函数传递任何参数,所以我想知道,有什么区别:
window.onload = initAll();
Run Code Online (Sandbox Code Playgroud)
和
window.onload = initAll;
Run Code Online (Sandbox Code Playgroud)
请解释其背后的原理.
当我们在按钮中执行内联命令时:
<button id="myButton" onclick="alert('Hi!')">
Run Code Online (Sandbox Code Playgroud)
为什么
document.getElementById("myButton").onclick = alert('Hi!')
Run Code Online (Sandbox Code Playgroud)
不起作用,但在页面加载时发出警报?我无法理解function()添加到它和没有它是如何工作的function().我希望你们理解我的问题.我在这里遗漏了一些东西.
我的按钮上有用于Google Analytics(分析)的代码。我只需要执行一次它,这样用户就不能通过多次按下按钮来更改统计信息。我尝试了类似主题的解决方案,但是它们不起作用。请帮忙。这是我的代码。
<script>
function klikaj(i) {
gtag('event', 'first-4', {
'event_category' : 'cat-4',
'event_label' : 'site'
});
}
document.body.addEventListener("click", klikaj(i), {once:true})
</script>
<div id="thumb0" class="thumbs" onclick="klikaj('rad1')">My button</div>
Run Code Online (Sandbox Code Playgroud) 众所周知,以下内容不会运行a()函数,因此不会出现警告框
// 1st
function a() {
alert('A!');
return function() {
alert('B!');
};
};Run Code Online (Sandbox Code Playgroud)
我们知道以下代码将运行a()函数和警告框'A!' 会出现
// 2nd
function a() {
alert('A!');
return function() {
alert('B!');
};
};
a(); // calling functionRun Code Online (Sandbox Code Playgroud)
但是,如果我们运行以下代码,将调用a()函数并且警告框'A!' 也会出现,就像上面的第二个代码一样
// 3rd
function a() {
alert('A!');
return function() {
alert('B!');
};
};
var x = a(); // assigning function to new variableRun Code Online (Sandbox Code Playgroud)
问题: 为什么会发生这种情况(第3段)?我们还没有调用a()函数(我目前的理解).我们不是只是将x分配给()函数吗?
如果我使用setTimeout()和setInterval()调用命名函数而不使用括号,它将按预期工作.当我用括号调用相同的函数时,它会立即执行它或者给出错误.
我正在寻找一个更深入的了解这个问题,然后我在网上找到了什么.你们能告诉我为什么这是真的吗?
var func = function(){
console.log("Bowties are cool.");
}
setTimeout(func(), 1500);
// Prints "Bowties are cool." immediately
setInterval(func(), 1500);
// Throws an error
setInterval(func, 1500);
// Works as expected
setTimeout(console.log("Bowties are cool."),1500);
// This method has the same result as "setTimeout(func(), 1500)".
Run Code Online (Sandbox Code Playgroud) 在 javascript 中有一个回调函数,函数是一流的对象,对吗?所以我可以传递一个函数作为参数但是如果我传递 console.log() 它不起作用为什么它也不是一个函数?
setTimeout(function(){console.log("hello")},5000);
Run Code Online (Sandbox Code Playgroud)
但是这个代码是有效的
setTimeout(console.log("hello"),5000);
Run Code Online (Sandbox Code Playgroud)
产生错误,这是为什么?
在 onPressed 或 Ontap 上调用不带括号的函数和带括号的函数有什么区别?
我只知道不能在 onPressed 上使用括号调用 void 函数。
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
Run Code Online (Sandbox Code Playgroud)
_incrementCounter 有 void 返回类型
void _incrementCounter() {
setState(() {
_counter++;
});
}
Run Code Online (Sandbox Code Playgroud)
但我没有找到任何适当的文件。
最近我需要使用 RxJS。我试图设计一个错误处理流程,但我发现了一些奇怪的语法传递方法参数:
.subscribe(
x => {
},
console.warn // <- Why does this compile, and warn 'is not 7' in debug console?
);
Run Code Online (Sandbox Code Playgroud)
https://stackblitz.com/edit/rxjs-6-5-error-handle-no-arrow-issue
,console.warn,不喜欢,error => { console.warn(error); }如果没有箭头函数,它仍然会将错误传递给 console.warn。为什么?
代码:
import { throwError, concat, of } from "rxjs";
import { map } from "rxjs/operators";
const result = concat(of(7), of(8));
getData(result).subscribe(
x => {
console.log("typeof(x)", typeof(x));
if (typeof(x) === 'string') {
console.log("x Error", x);
return;
}
console.log("no …Run Code Online (Sandbox Code Playgroud) 我有一个返回 Promise 的函数。.then()当我在or块中使用完 Promise 后.catch(),我总是想执行相同的清理代码。我当前的设置是这样的:
const promiseWithFinally = () => {
return new Promise((resolve, reject) => {
// resolve or reject at some point
}).finally(() => console.log('finally done'))
}
promiseWithFinally()
.then(() => console.log('then done'))
.catch(() => console.log('catch done'))
Run Code Online (Sandbox Code Playgroud)
我想要发生的是要么then done先catch done登录,然后再登录finally done。然而,它似乎以完全相反的顺序执行 - 当我在 5 秒超时后解析 Promise 时,finally done首先在 5 秒后记录,然后then done立即记录。
我做错了什么或者一般可以这样做吗?我知道我可以将 附加.finally()到每个单独的函数调用中,但由于它总是相同的,所以我想将它放在函数定义中。
javascript ×9
angular ×1
dart ×1
dom ×1
dom-events ×1
flutter ×1
html ×1
onclick ×1
promise ×1
rxjs ×1