javascript 为什么 window.addEventListener 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17217983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why window.addEventListener does not work?
提问by 101V
On the window.onload event, I would like a function to be called. If I define it as given below, the function does not get called.
在 window.onload 事件上,我想要调用一个函数。如果我如下定义它,则不会调用该函数。
try {
window.addEventListener("load", initialiseTable, false);
} catch(e) {
window.onload = initialiseTable;
}
var initialiseTable = function () {
console.log("hello world!!");
};
but if I change the function declaration to
但是如果我将函数声明更改为
function initialiseTable() {
console.log("hello world!!");};
it works, any idea?
它有效,有什么想法吗?
采纳答案by taiansu
While you using var x = function(){}
to declare functions, it should be declare before you call the function.
使用var x = function(){}
声明函数时,应在调用函数之前先声明。
By function x (){}
, x
will exist in current scope, no matter how later it declare. Because such side effect, the var x = function(){}
are more recommended. For example:
By function x (){}
,x
将存在于当前作用域中,无论它声明多晚。因为这样的副作用,var x = function(){}
比较推荐。例如:
var user = 'alien';
if( user == 'alien') {
function salute () { console.log("Welcome to Earth!"); }
} else {
function salute (){ console.log("Good day!"); }
}
The salute()
will print Good day!
anyway, even though it not we want at all.
在salute()
将打印Good day!
无论如何,即使它不是我们想要的。
回答by Edorka
Declare the function before.
先声明函数。
var initialiseTable = function () {
console.log("hello world!!");
};
try {
window.addEventListener("load", initialiseTable, false);
} catch(e) {
window.onload = initialiseTable;
}
For more information about declaring a function read this: var functionName = function() {} vs function functionName() {}
有关声明函数的更多信息,请阅读: var functionName = function() {} vs function functionName() {}
回答by Mystic
Just put the function initialiseTableto the first position, eg:
只需将函数initialiseTable放在第一个位置,例如:
var initialiseTable = function (){ /*[...]*/ }
After continue calling.
之后继续调用。
回答by Esailija
Since you come from C#, the difference is a lot like:
因为你来自 C#,所以区别很像:
class A {
public int method2() {
//I can call method1 even though it *declared* later below
int a = method1();
}
public int method1() {
}
}
vs
对比
class B {
public int method2() {
bool a = method1(3); //I can't call it because it is *assigned* later below.
//In fact I cannot even compile it
Func<int, bool> method1 = x => x == 3;
}
}
You may notice the difference in javascript, if you look at:
如果您查看以下内容,您可能会注意到 javascript 的不同之处:
function a() {
}
There is no assignment anywhere.
任何地方都没有任务。
Where as with var a = function(){}
there is obviously assignment and it is similar to the latter C# example.
哪里var a = function(){}
有明显的赋值,它类似于后一个 C# 示例。
回答by Vivek Sadh
Function declaration should always precede its call.
函数声明应始终在其调用之前。
At this line:-
在这一行:-
window.addEventListener("load", initialiseTable, false);
initialiseTable is not known so it should be placed after function definition. Actually function statement causes its identifier to be bound before anything in its code-block* is executed.Function declarations loads before any code is executed. It differs from the function expression which is evaluated in normal top-down order. Function expressions loads only when the interpreter reaches that line of code. so if you use:-
initialiseTable 是未知的,所以它应该放在函数定义之后。实际上,函数语句会在执行其代码块* 中的任何内容之前绑定其标识符。在执行任何代码之前加载函数声明。它不同于按正常自上而下顺序计算的函数表达式。函数表达式仅在解释器到达该行代码时加载。所以如果你使用:-
var initialiseTable = function () {
console.log("hello world!!");
};
it won't work but this works:-
它行不通,但这行得通:-
function initialiseTable() {
console.log("hello world!!");};