Javascript basics-function Type (I)

Source: Internet
Author: User


In js, the function type is actually an object, and each function is an instance of the function type. It has the same attributes and methods as other reference types.

Statement

1 Normal Mode


Function sum(num1,num2){Return num1+num2;}

2. Use variables to initialize Functions


Var sum=function(num1,num2){Return num1+num2; }


The above two methods are commonly used. Of course there are other methods, but they are better than the above.

Define a function for ease of use. Here, we only want to briefly understand the internal attributes.

Internal Function Attributes

Any function has two special objects,Arguments and this

Arguments is an array object that contains all the parameters passed. This is the scope of its object.

Arguments

For example

A recursive function calls Functionsum (num) {If (num <= 1) {Return 1 ;}else {Return num * box (num-1 );}}

There is no problem with the sum. If the function name is modified, the recursive function will report an error. To solve this problem, we can use the function's own Arguments. calleeSolution.


Function sum(num){If(num<=1){      Return 1;}else{Return num*arguments.callee(num-1);}}

This usage

I thought I understood it very well before, But I still see deviations in the following source code.

Window object is jsThe largest and most peripheral object in.

All global variables are the properties of window. So there is

Varcolor = 'red'

Alert (this. color );

Alert (window. color)

The two are the same results. Here color is a global variable, so it is the property of window. This indicates windows

Window. color = 'red'

Var box = {Color: 'Blue ', sysColor: function () {Alert (this. color) ;}} alert (this. color); // here, this is the window object, so it is windowcolorbox. sysColor (); // the color of the box

========================================================== ====================================

Window. color = 'red' Function sayColor () {Alert (this. color); // here is a dynamic this, pay attention to the changes in the range} sayColor (); Var box = {Color: 'Blue '} Box. sayColor = sayColor; Box. sayColor (); // blue

The above basic understanding of this is also the most commonly used range transformation. this represents the surrounding object itself.

Call () and apply () Methods

On the surface, they call other functions, but do not be confused by their appearances. The real function isModify the function Scope

Var color = 'red'; Var box = {Color: 'Blue ';} Function sayColor () {Alert (this. color);} saycolor (); // red sayColor. call (this); // this is window // red sayColorcall (box); // blue; impersonate box, And the scope is in the box object.


Call. The apply parameter is

Apply (object scope, parameter );

The function definition is called at the end. What types of calls are called,

Function call

1 As the return value of the Function

Same as common variables.

function box(sum,num){returnsum+num;        } function sum(num){returnnum+10;} varresult=box(sum(10),10);alert(result);


Here we can see that the output result is 30, and here sun returns a specific value.

Passing functions as parameters

Modify the preceding functions

function box(sum,num){return sum(num);        } function sum(num){returnnum+10;} var result=box(sum,10);alert(result);

The output value is 20, or a value, but the sum parameter here is no longer a specific value, but a specific function. In the box function, the parameter is a sun function.

Summary:

This content is an understanding of the basic attributes and methods of funtion. js functions are familiar with functions in other languages and are also derived from the object type, there are also parameter groups such as this and arguments. Compared with the main function, the functions in j2ee and other languages have different types of attributes, such as the unspecified length and property attributes.

Property prototype. For more information, see function introduction.

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.