The difference between function declaration in JavaScript and function Expression or function fn () {} and var fn=function () {}

Source: Internet
Author: User
Tags closure

JavaScript is an interpreted language, and function declarations are interpreted after the JavaScript code is loaded and executed, and the function expression is interpreted only when it executes to this line of code.

There are two ways to define a function in JS,

1 Yes: var aaa=function () {...}

2 Yes: function aaa () {...}

The Var method defines a function that cannot be called before the function is declared, only the function can be declared first and then called.

function methods define functions that can be called first and then declared.

The Var func=function and function func () do not have any difference in meaning, but their interpretation precedence is different: the latter is preceded by other statements at the same statement level.
That is: {var k = xx (); function xx () {return 5;}}  No error, and {var k = xx (); var xx = function () {return 5;}} The error will occur.

Let's take a look:

var p=function() {} ();

What does this piece of code mean.

After reading the following examples, everyone will be at a glance.

var function () {return ' abc ';} (); Alert (p); // ABC alert (typeof p);//string
var function () {return 111;} (); Alert (p); // 111 alert (typeof p);//number

Now that's clear, it's actually defining a variable, which is the return value of the subsequent function.

With JavaScript two features, but also the implementation of the JS engine must lead to:

1) return value. In the JS engine, all syntax, operations have a return value, and usually the return value is itself or undefined. usually we can use the "()" operator to get the return value of the current sentence (some operators are not available, such as Var). For example:a=3, in fact, the return value of this line is 3, so in a=b=3, the JS engine can be executed correctly, first 3 is assigned to B, and then the current return value 3 is assigned to a (instead of what you think, the first 3 is assigned to B, and then B is assigned to a). Another example is the function var fun=function () {}, when the JS engine executes to this line, the right is executed first, the return value is the literal of a function, and then the function literal is assigned to fun.

jquery also takes advantage of this feature (function return this), so there is a chain call. Let's look at an example: "FontFamily". Replace (/([A-z])/g, "-$1"). toLowerCase (), yes. The return value is also used here, so the result of this line is: font-family.

2) closures. A child function calls a variable (non-argument) of the parent function, which is the closure. In JS, the child function can access the variables of the parent function, when the referenced variable is not freed, and the child function can always hold a reference to the variable.

Combining these two features, you can generate a powerful geek:

(function () {}) ();

Do not know the professional name, I call it: immediately execute the anonymous function.

First, within the first pair of parentheses, is an anonymous function, and the second parenthesis immediately invokes the return value of the anonymous function, where the contents of the anonymous function are immediately executed.

The power of this geek is that it is independent of a scope (the contents of the parentheses are immediately reclaimed after execution), the internal variable is inaccessible outside, and it can access the external variables through the this reserved word.

As an example:

var i=100; (function () {var j=1; this.plus_ij = function () {j+=i; alert (j);}}) (); Plus_ij ();

Plus_ij ();

In the example above, an anonymous function is immediately executed, which can be accessed to the external variable i, and even Var can define an I (without affecting the external I value). and the internal variable J forms a closure that is not released.

(function () {var a = function () {} function B () {} THIS.C = function () {a (); B ();}}) (); C ();

In the example above, you will find that, regardless of the way you define it, you cannot access the A and B functions immediately outside of the anonymous function. That is, the A and B functions act as intrinsic functions for the immediate execution of an anonymous function that cannot be called externally except through an external function that is immediately defined inside the anonymous function.

Transferred from: http://www.cnblogs.com/yongtaiyu/articles/3200722.html

The difference between function declaration in JavaScript and function Expression or function fn () {} and var fn=function () {}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.