Summary of JS function

Source: Internet
Author: User

Definition of a function

(1) Declaration of functions: function Add (x, y) {return x + y;} (2) function expression var add = function (i, j) {reutrn i + j;} (3) Object instantiation (the actual programming is generally not used), defined in the global. var add = new Function (' I ', ' J ', "return (i + j)");


Properties of the function:
Prototype:add
Constructor:function Add (i, J)
__proto__: Object, from the prototype of the instance initialization


constructor function:
Prototype is the patent of the function, only the function has the prototype attribute.
The difference between a constructor and a normal function
(1) There is no difference in nature.
(2) Constructors typically have this specified instance property, and there are usually some common methods above the prototype object
(3) constructor naming is usually capitalized in the first letter.


Call to function
(1) Constructor call pattern
var person1 = new Person (' HG ');
var person2 = new Person (' WJ ');

(2) method invocation mode is the method that invokes the object
Person1.say ();

(3) Function call mode

Example (1) function aaa () {THIS.A = 1;        Alert (this + "AAA" + THIS.A);        function bbb () {alert (this + "BBB" + THIS.A);    } BBB (); }//here can pop up 1. Because THIS.A = 1, the This = = window.    So a hangs on the window. AAA ();//Note: The two this point is the window. Example (2)//Get Object constructor name function type (obj) {return obj && obj.constructor && obj.constructor.toSt    Ring (). Match (/function\s* ([^ (]*)/) [1];        } function AAA () {this.aa = 1;        Alert (this + "AAA" + THIS.AA);        THIS.BBB = function () {alert (type (this)),//AAA alert (this + "BBB" + THIS.AA); }} var A = new aaa ();//[object Object] AAA 1 a.bbb ();//[object Object] BBB 1 alert (window. A.AA);//1 alert (WINDOW.AA);//undefined Example (3)//Get Object constructor name function type (obj) {return obj && obj.co    Nstructor && obj.constructor.toString (). Match (/function\s* ([^ (]*)/) [1];     } function AAA () {this.aa = 1;   Alert (this + "AAA" + THIS.AA);      function bbb () {Alert (type (this));        Window alert (this + "BBB" + THIS.AA);      } BBB ();  [Object Window] bbb undefined} var A = new AAA (); [Object Object] AAA 1//A.BBB (); cannot be called because it is the BBB local function, alert (window.     A.AA);       1 alert (WINDOW.AA); Undefined

(4) Apply,call call mode

Function.prototype.apply;
Apply is the method above the function. All functions can call this method.

If you get a function of an object type:
Object.prototype.toString.apply (obj);

Object.prototype.toString.apply (1234);//number

Apply is a borrowed feature.

A.apply (B, args);//b function-->> borrows-->>a function. Operation parameter args.

Call (this, x, y),//call, and the arguments passed in separately, and apply is an array.

(5) Bind Method!!!
var test = B.bind (A, Argus);
Test ();
Bind binds objects and parameters. Returns a function that, when needed, executes just fine.


Two, arguments

(1) Array-like
Just look like an array, but not an array, can have arguments[index]
and Arguments.length methods, but other array methods are not available.

function test (x, Y, z) {
Alert (Type (arguments)); Object
  var args = Array.prototype.slice.apply (arguments);
Alert (type (args)); Array
}

Test (1,3,4,5,6);
Pop-up: The first is an object and the second is an array of arrays.

Use this to convert the parameter to an array, or you can.
var args = Array.prototype.slice.apply (arguments);


(2) Arguments.callee
Point to the function itself. Arguments.callee (); Call the function itself.
In a recursive manner.

var hga = 3;function Test (a) {    if (a! = 0) {        alert (a);        a--;    }    else {        return;    }    Arguments.callee (a);} Test (HGA);

Three, recursion

Do not Arguments.callee (); It is necessary to prevent, the name of the function is tampered with.

Four, closure: Defines a function in a function, and a function that is defined, invokes a variable in the parent function.

(function () {var a = 0;function B () {a = 1;debugger;}}) (); Closure usage scenario: Changing the scope, turning the function's variable into a private variable.

The post will be devoted to writing a question about closures.

Five, first-class function.

JS function, can be stored as a variable, can be passed as a parameter, can be returned as a return value.

(1) Granulation of functions. such as implementing an Add (NUM1) (num2) (num3), the function of this chain call. A non-quantitative, cumulative. Callback.  function Add (value) {var helper =  function (next) {value = typeof (value) = = = ' undefined '? Value:value + Next;return Helper;} helper.valueof = function () {return value;} return helper;} Console.log (Add (1) (2) (3));//The way to do this is to first define a function within the function, helper, to determine if the incoming parameter is undefined.//if so, the value is unchanged, if not, the value is updated. The function returns a helper function. (2) Callback, do asynchronous callback in Ajax, when success returns success, do function ()//encapsulate an AJAX asynchronous call. function Ajax.get (URL, callback) {var createxhr = function () {var xhr;if (window). XMLHttpRequest) {xhr = new XMLHttpRequest ();} else if (window. ActiveXObject) {xhr = new ActiveXObject (' Microsoft.XMLHTTP ');} return XHR;} var xhr = CREATEXHR (); if (XHR) {xhr.open (' get ', url, true); Xhr.onreadystatechange = function () {if (xhr.readystate = = 4) {if (Xhr.status = =) {callback.success (XHR);} else {callback.fail (XHR);}}} Xhr.send (null);}}

Summary of JS 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.