I am the first in the world: functions

Source: Internet
Author: User
Tags types of functions

May 1995, the American programmer Brendan Eich only spent 10 days, completed the design of JavaScript, function is the first class of the language of citizens. Once launched, Netscape quickly ruled the entire browser market.

In December 1997, Chinese singer Andy Lau was not satisfied, so he wrote a song sung in Minnan, called "I am the first in the world". It is a pity that Andy Lau is still not even a Hello world to write.

  

Distressed Andy ...

But to return to the subject, why do we often say that the function is the first in the JavaScript world?

General programming language, the first class of objects have the following features:

    1. Can be created by literal
    2. Properties that can be assigned to variables, array elements, and other objects
    3. Can be passed as a parameter to a function
    4. Can be used as the return value of a function
    5. Can contain properties that can be dynamically created and assigned

  JavaScript functions have all of the above capabilities and can be used like other objects. Therefore, we say that a function is the first class of objects. Here is a simple demo:

    functioncivics1 () {//can be created by literal    }            varFoo = {"value": ' Bar '}; Civic2= foo;//be assigned to a variableCIVICS1 = Foo.value;//attributes that are assigned to an objectCIVICS1 = [n/a];//is assigned to an array        functioncivics2 () {Console.log ("Yeah"); }    functionCallback (FN) {Console.log ("I am The first!"); fn ();//Sometimes you use call or Apply} callback (CIVICS2);//Pass as parameter        functionclosure () {varFree = '; return function(){            returnFree + 1; };//as the return value} civics2.attr= ' created ';//property can be assigned a valueConsole.log (CIVICS2.ATTR);

As you can see, the function, in addition to having all the functions of the object, can be called when it distinguishes the object.

There are three types of functions: normal functions, inline functions, and anonymous functions.

There are four ways to call a function:

    1. Call directly
    2. Method invocation as an object
    3. Called as a constructor for an object
    4. Called by apply () or call ()

When you call a JavaScript function, the number of parameters (parameter) and arguments (augment) does not get an error.

    • If the argument is more than the formal parameter, the extra part is ignored.
    • If there are more formal parameters than arguments, the values that are not assigned are set to undefined.

All function calls will have two implicit formal parameters: arguments and this

    • Arguments: An array but not an array. It has the length property, gets the arguments, and can also access the first element with index, for example, Arguments[0], but there are no other methods that the array has.
    • This: function context, which depends on how it is called

The way the first and second invocations of a function (direct invocation and invocation of an object method) are actually the same. Because in the browser, the first is actually the method of the Window object, if the function is a global function.

Speaking of the third, here is the extension of the constructor, ECMAScript (ES6) specification has supported Class (class), and also support extend (inheritance), class based on the constructor method to create and initialize the object, the MDN example is as follows:

class Square extends Polygon {constructor (length) {//Here , it calls the parent class ' constructor with lengths    //provided for the Polygon ' s width and heightsuper (length, length); //note:in derived classes, super () must be called before    //can use ' this '. Leaving this out would cause a reference error.     This. Name = ' Square '; } get Area () {return  This. Height * This. Width; } set Area (value) { This. Area =value; } }

After all, the mainstream browser is not compatible, so go back to ES5: Call the constructor with the new keyword, after using the constructor call, the following occurs:

    • A new object is created
    • This object is passed to the constructor as the this parameter and becomes the function context of this constructor
    • Without an explicit return value, the new object is returned as the value of this constructor

The purpose of the constructor is to create a new object, initialize it, and return it as a constructed value.

The last one, apply () and call (). When we call a function, they are used to display the context in which any object is specified as a function. The difference between apply () and call () is that only the second parameter of apply () is an array, and call () is a string of separate elements.

I am the first in the world: functions

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.