02.JavaScript Object-oriented essentials--functions

Source: Internet
Author: User


In JavaScript, a function is actually an object. A function that differs from other objects is characterized by:
The function has an internal property called [[Call]]. The [[Call]] property is unique to the function,
Indicates that the object can be executed. ECMAScript defines the typeof operator pair with [[Call]]
property returns "function" for the object. Understanding the behavior of a function is the core of understanding JavaScript.

I. Function declarations and expressions
1.1 Function declarations
followed by the function's name at the beginning of functions

  function Add (num1,num2) {       return num1 + num2;  }


The function declaration can be promoted in JavaScript because the engine knows in advance the function's
Name. The function expression can only be referenced by a variable and therefore cannot be promoted.

1.2 Function expressions
A function name is not required after the start of a function, which is called an anonymous function, but is
A variable reference.

var function (NUM1, num2) {       return num1 + num2;  };


1.3 function declaration and expression Differences

Add (5,5); // return ten  ADD2 (10,10)// Error: ADD2 is not a function  function  Add (num1,num2) {         return NUM1 + num2;  }   var function (num1,num2) {       return num1 + num2;  };  ADD2 (5,5)// return ten;

Two. The function is the value
Functions are a major focus of JavaScript, and you can use functions like objects.
You can also assign them to variables, add them to the object, and pass them as arguments to
Other functions, or return from another function. Basically, if you can use other reference values
, you can use the function.

1 functionSayhi () {2Console.log ("hi!");3   }4Sayhi ();//output "hi!"5   varSayHi2 =Sayhi;6SayHi2 ();//output "hi!"7   //use constructors to implement:8   varSayhi =NewFunction ("Console.log (\" hi!\ ");");9Sayhi ();//"hi!"Ten   varSayHi2 =Sayhi; OneSayHi2 ();//"hi!" A   //Pass the function as a parameter -   varnumbers = [1,5,8,4,7,10,2,6]; -Numbers.sort (function(first,second) { the        returnFirst-second; -   }) -Console.log (numbers);//"[1,2,4,5,6,7,8,10]" - Numbers.sort (); +Console.log (numbers);//"[1,10,2,3,4,5,7,8]"

Three. Function parameters
Another unique thing about JavaScript functions is that you can pass them to a function
Any number of parameters does not cause errors. That's because the function parameters are actually saved in the
An object of class array arguments. The length property of the arguments will tell
How many values do you currently have? Array.isarray (arguments) returns false forever.
The function is also an object, so there are properties, and the Lenght property returns the function expected parameter
Number.

3.1 Arguments Object

1  functionreflect (val) {2     returnVal;3    }4Console.log (Reflect ("hi!"));//"hi!"5Console.log (Reflect ("hi!", 25));//"hi!"6Console.log (reflect.length);//"1"7 8reflect =function(){9     returnArguments[0];Ten    } OneConsole.log (Reflect ("hi!"));//"hi!" AConsole.log (Reflect ("hi!", 25));//"hi!" -Console.log (reflect.length);//"0"

3.2 Overloading
JavaScript is not overloaded, but it can simulate overloading.
The execution function body is judged by the number of arguments passed in Arguments.length.

function Sayhi (msg) {    if(argrments.length = = = 0) {     = "Default msg";    } Else {     console.log (msg);}    }   Sayhi (' hello '); // output "Hello"

Four. Object methods
If the property of an object is a function, the function is called a method.

var person = {   ' Nicholas ',   function() {    Console.log (Person.name);   }}

  4.1 this to bear
   javascript all function scopes have a This object that represents the object that invokes the
    function. This represents the Global Object (window) in the global scope.
    When a function is called as a method of an object, the value of this default is equal to that
    object.
   

function say () {    Console.log (this. name);}    var person1 = {    ' Nicholas ',    saynmae:say   }   var person2 = {    ' Greg ',    syaname:say   }   //  ' Nicholas   ' // "Greg"

4.2 Change this (call (), apply (), bind ())
There are 3 methods of function that allow you to change the value of this. Remember: functions are also objects
So the function also has a method.

4.2.1 Call (the value of this, ' parameter 1 ', ' parameter 2 ') method

    functionsay (label) {console.log (label+ ":"+ This. Name); }    varPerson1 ={name:' Nicholas '    }; varPerson2 ={name:' Greg '    }; varname = ' Micheel '; Say.call ( This, ' Global ');//output "Global:micheel"Say.call (Person1, ' person1 ');//output "Person1:micholas"Say.call (Person2, ' person2 ');//output "Person12:greg"

4.2.2 Apply (this value, [' Parameter 1 ', ' parameter 2 ') ' method

    Say.apply (this// Output "Global:micheel"    say.apply (person1,[' Person1 ']);   Output "Person1:micholas"    say.apply (person2,[' Person2 ');   Output "Person12:greg"

4.2.3 Bind () method

    var say1 = say.bind (person1);    Say1 (// Output "Person1:micholas"    var say2 = Say.bind (Person2, "Person2");     // output "Person12:greg"


02.JavaScript Object-oriented essentials--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.