Standard reference function declarations and function expressions
There are two ways of defining a function: function declaration and function expression.
-
function declaration:
-
function Identifier (formalparameterlist
Opt
) {Functionbody}
-
function expression:
-
function Identifier
Opt
(FormalParameterList
Opt
) {Functionbody}
ECMAScript the function declaration and function expression according to the context, suppose "function test () {}" is part of an expression, it is a function expression, otherwise it is a function declaration.
For more information about function declarations and function expressions, refer to the contents of the ECMAScript specification in the Definition .
Identifier
In ECMAScript, variable names, object properties, or method names are identifiers, identifiers can contain English letters, numbers, dollar sign ' $ ', and underscore ' _ ', but cannot start with a number and cannot be reserved words.
In other words, a symbol such as ' + ', '. ' cannot appear in the identifier.
For more information about identifiers, refer to the contents of the ECMAScript specification 7.6 Identifier Names and Identifiers .
Problem description
The implementation of the function declaration and function expression of IE has something to do with the ECMA specification.
In IE:
The identifier in a function declaration allows an expression that contains the '. ' operator. (When you bind an event, you can replace the '. ' With the ':: ' Before the event name. )
Identifiers in function expressions can be accessed outside of the function.
The impact
Code that relies on the above "features" of IE will not perform as expected in other browsers.
For example, an event handler defined in the form of ' function Window.onload () {} ' is only valid in IE and will be reported as a syntax error in other browsers.
The affected browser
Problem Analysis 1. The identifier in a function declaration allows an expression that contains the '. ' operator. (When you bind an event, you can replace the '. ' With the ':: ' Before the event name. )
Analyze the following code:
function A () {}function a.prototype.b () {}var a=new A (); alert (typeof a.b);
In this case, the identifier for the second function declaration is an illegal identifier, which contains the '. ' operator.
Each browser behaves as follows:
IE6 IE7 IE8 |
Other Browsers |
function |
Browser error (Syntax errors) |
Visible: The above wording only IE support.
2. Identifiers in function expressions can be accessed outside of the function.
Analyze the following code:
var a=function b () {};alert (typeof B);
According to the provisions in the ECMA specification, the identifier B in the above function expression will be accessible only within the function's scope, that is, B is only valid for the function scope and cannot be accessed externally.
Each browser behaves as follows:
IE6 IE7 IE8 |
Other Browsers |
function |
Undefined |
Visible: IE does not follow the specification, and identifiers in function expressions can also be accessed within the scope of the function's perimeter.
Note: The contents of this article are referenced in the article: Named function Expressions demystified (Chinese translation: The quest for named function expressions).
Solution Solutions
Avoid using these "features" of IE to ensure compatibility with all browsers.
See Knowledgebase
Related issues
Test environment
Operating system version: |
Windows 7 Ultimate Build 7600 |
Browser version: |
IE6 IE7 IE8 Firefox 3.6 Chrome 4.0.302.3 Dev Safari 4.0.4 Opera 10.51 |
Test page: |
... |
This article was updated when: |
2010-07-02 |
Key words
Named function Declaration expression function declaration expressions
Reproduced
http://w3help.org/zh-cn/causes/SJ9001
IE6 IE7 IE8 The implementation of function declarations and function expressions differs from other browsers