JavaScript Learning note "Five"

Source: Internet
Author: User

Reference type of JavaScript-function

First, Function

1. Declaration and function expressions of functions,

In JavaScript, the function is also a type, all defined functions are of type

     

Alert (SUM (10, 1));

function sum (val1, val2) {

return val1 + val2;

}

The above method does not error, because the JavaScript execution environment will advance the declaration of the function, and the lower function expression will be error, because when the alert is executed, sum has not obtained a reference to the function,

That is, JavaScript's execution environment will only advance the declaration of a function, but not advance the assignment

Alert (SUM (10, 1));

var sum = function (Val1, val2) {

return val1 + val2;

};

There is also a way to create a function, which is not recommended here, because it requires one resolution and one run, which can also degrade JavaScript performance,

var function = new function (' Val1 ', ' val2 ', ' return val1 + val2 '); The last parameter is the method body, the front is the parameter of the method,

2. Functions can be passed as values,

The function name stores a reference to the function,

function sum (val1, val2) {

return val1 + val2;

}

function CallFunction (func, NUM1, num2) {

return func (NUM1, num2);

}

3. Internal properties of the function

There are two special objects inside the function arguments, and this

Arguments is an array of parameters of the function,

function sum (NUM1, num2) {

return arguments[0] + arguments[1]; Here Arguments[0] and arguments[1] represent NUM1 and num2, respectively,

}

Arguments also has an attribute callee, which is a pointer to the function where the arguments is located, the Sum function,

function Factory (num) {

if (num < 0) {

return 0;

} else {

return num + arguments.callee (num-1); Here we use the callee, when we want to replace the factory function name, we do not have to worry about the internal problem of the function,

}

}

This is another special object inside the function, the This object refers to the object of the execution environment on which the function depends,

var window.color = ' Red ';

var o = {color: ' Blue '};

function Saycolor () {

alert (This.color);

}

Saycolor (); ' Red '

O.saycolor = Saycolor;

O.saycolor (); ' Blue ';

4. Properties and methods of functions, length, prototype

function sum (NUM1, num2) {

return NUM1 + num2;

}

alert (sum.length); Length represents the number of arguments for the function sum,

The prototype property of the function holds all methods of the sum instance: var s = new sum (); The properties and methods of the object s are the methods that point to the prototype property of Sum,

Note ECMAScript 5 also defines a new amount method, which creates an instance of a function whose this value is bound to the value passed to the BIND function,

var o = {color: ' Blue '};

function Saycolor () {

alert (This.color);

}

Saycolor = Saycolor.bind (o);

Saycolor (); ' Blue '

The BIND function does not alter the original Saycolor's application to this, but instead returns the function that points to O,

JavaScript Learning note "Five"

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.