Javascript 更改js函数的定义在某些浏览器中不起作用
我搜索了这个问题的答案,但没有找到任何答案,尽管肯定有一个简单的解释。下面的函数js在不同的浏览器中显示不同的结果…有人能告诉我为什么吗?非常感谢Javascript 更改js函数的定义在某些浏览器中不起作用,javascript,function,internet-explorer,Javascript,Function,Internet Explorer,我搜索了这个问题的答案,但没有找到任何答案,尽管肯定有一个简单的解释。下面的函数js在不同的浏览器中显示不同的结果…有人能告诉我为什么吗?非常感谢 var i='a'; if (i=='a') function theFunction(){alert('hi');} else function theFunction(){alert('bye');}; theFunction(); //ff results in hi //ie results in bye //chro
var i='a';
if (i=='a') function theFunction(){alert('hi');}
else function theFunction(){alert('bye');};
theFunction();
//ff results in hi
//ie results in bye
//chrome results in bye
问题是在
if
块中使用函数声明
发件人:
注意:大家都知道ECMAScript的几个广泛使用的实现
支持将FunctionDeclaration用作语句。然而
在不同的国家之间存在着显著且不可调和的差异
应用于此类应用的语义中的实现
功能声明。由于这些不可调和的差异
将FunctionDeclaration用作语句会导致代码
在实现之间不能可靠地移植。建议
ECMAScript实现或者不允许使用
函数声明或在禁用此用法时发出警告
遇到。ECMAScript的未来版本可能会定义替代版本
在语句上下文中声明函数的可移植方法
如果你试图在严格模式下使用你的代码,你会得到
SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function
相反,您可以使用
var i='a',
theFunction;
if (i=='a') theFunction = function(){alert('hi');}
else theFunction = function(){alert('bye');};
theFunction();
或者,如果您的代码足够简单(如上面的示例),请使用三元运算符:
var theFunction = i=='a'
? function(){alert('hi');}
: function(){alert('bye');};
问题是在
if
块中使用函数声明
发件人:
注意:大家都知道ECMAScript的几个广泛使用的实现
支持将FunctionDeclaration用作语句。然而
在不同的国家之间存在着显著且不可调和的差异
应用于此类应用的语义中的实现
功能声明。由于这些不可调和的差异
将FunctionDeclaration用作语句会导致代码
在实现之间不能可靠地移植。建议
ECMAScript实现或者不允许使用
函数声明或在禁用此用法时发出警告
遇到。ECMAScript的未来版本可能会定义替代版本
在语句上下文中声明函数的可移植方法
如果你试图在严格模式下使用你的代码,你会得到
SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function
相反,您可以使用
var i='a',
theFunction;
if (i=='a') theFunction = function(){alert('hi');}
else theFunction = function(){alert('bye');};
theFunction();
或者,如果您的代码足够简单(如上面的示例),请使用三元运算符:
var theFunction = i=='a'
? function(){alert('hi');}
: function(){alert('bye');};
问题是在
if
块中使用函数声明
发件人:
注意:大家都知道ECMAScript的几个广泛使用的实现
支持将FunctionDeclaration用作语句。然而
在不同的国家之间存在着显著且不可调和的差异
应用于此类应用的语义中的实现
功能声明。由于这些不可调和的差异
将FunctionDeclaration用作语句会导致代码
在实现之间不能可靠地移植。建议
ECMAScript实现或者不允许使用
函数声明或在禁用此用法时发出警告
遇到。ECMAScript的未来版本可能会定义替代版本
在语句上下文中声明函数的可移植方法
如果你试图在严格模式下使用你的代码,你会得到
SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function
相反,您可以使用
var i='a',
theFunction;
if (i=='a') theFunction = function(){alert('hi');}
else theFunction = function(){alert('bye');};
theFunction();
或者,如果您的代码足够简单(如上面的示例),请使用三元运算符:
var theFunction = i=='a'
? function(){alert('hi');}
: function(){alert('bye');};
问题是在
if
块中使用函数声明
发件人:
注意:大家都知道ECMAScript的几个广泛使用的实现
支持将FunctionDeclaration用作语句。然而
在不同的国家之间存在着显著且不可调和的差异
应用于此类应用的语义中的实现
功能声明。由于这些不可调和的差异
将FunctionDeclaration用作语句会导致代码
在实现之间不能可靠地移植。建议
ECMAScript实现或者不允许使用
函数声明或在禁用此用法时发出警告
遇到。ECMAScript的未来版本可能会定义替代版本
在语句上下文中声明函数的可移植方法
如果你试图在严格模式下使用你的代码,你会得到
SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function
相反,您可以使用
var i='a',
theFunction;
if (i=='a') theFunction = function(){alert('hi');}
else theFunction = function(){alert('bye');};
theFunction();
或者,如果您的代码足够简单(如上面的示例),请使用三元运算符:
var theFunction = i=='a'
? function(){alert('hi');}
: function(){alert('bye');};
可以从条件表达式返回函数:
var i='a';
var theFunction=(function(){
if (i=='a') return function theFunction(){alert('hi');}
else return function theFunction(){alert('bye');};
})();
theFunction();
/*返回:“hi”*/您可以从条件表达式返回函数:
var i='a';
var theFunction=(function(){
if (i=='a') return function theFunction(){alert('hi');}
else return function theFunction(){alert('bye');};
})();
theFunction();
/*返回:“hi”*/您可以从条件表达式返回函数:
var i='a';
var theFunction=(function(){
if (i=='a') return function theFunction(){alert('hi');}
else return function theFunction(){alert('bye');};
})();
theFunction();
/*返回:“hi”*/您可以从条件表达式返回函数:
var i='a';
var theFunction=(function(){
if (i=='a') return function theFunction(){alert('hi');}
else return function theFunction(){alert('bye');};
})();
theFunction();
/*返回:hi'*/我非常喜欢这种自修改函数的模式,非常地道。 它的思想是利用函数声明只是一个带有早期评估的var声明(用几句话来说):
我非常喜欢这种自修改函数的模式,非常地道。 它的思想是利用函数声明只是一个带有早期评估的var声明(用几句话来说):
我非常喜欢这种自修改函数的模式,非常地道。 它的思想是利用函数声明只是一个带有早期评估的var声明(用几句话来说):
我非常喜欢这种自修改函数的模式,非常地道。 它的思想是利用函数声明只是一个带有早期评估的var声明(用几句话来说):
这将是导致问题的条件中缺少的curlybraces。它会在所有浏览器中为我发出“bye”警报?可能的重复情况将是导致问题的条件中缺少的curlybraces。它会发出“bye”警报在我的所有浏览器中?可能的重复是导致问题的条件中缺少的curlybraces。它会在我的所有浏览器中提醒“再见”?可能的重复是w