[Javascript-function] Functions Invocation/call (function calls) and call () and apply () methods

Source: Internet
Author: User
Tags javascript array

Description: The code in the JS function is executed when the function is called by Invoke.

The code does not execute when the function is defined,

The code inside the function is executed when the function is called.

The commonly used term is call a function rather than invoke a function.

function always belong to a object in JavaScript.

When the A function does no tbelong to nay object. In JavaScript there is alaways a default global object.

In HTML, it is the browser window itself. The global object would become a window function in this case.

That means it can is invoked by Window.functionname ().

1. This keyword: An object that represents the current code.

The value of this, when used in a function, is the OBJEC that owns the function.

Note that this is a variablke. It is a keyword. You cannot the value of the.

2. Invoking a Function as a Method

In JavaScript you can defiune functions as Object methods. (object method?), shaped like:

var myObject = {
FirstName: "John",
LastName: "Doe",
Fullname:function () {
return this.firstname + "" + this.lastname;
}
}
Myobject.fullname (); Would return "John Doe"

3. For JSON objects, you can use the "." To invoke a method within an object.

var myObject = {
FirstName: "John",
LastName: "Doe",
Fullname:function () {
Return this.firstname+ "" +this.lastname;
}
}

4.Invoking a function with a function Constructor

Note:

A constructor invocation creates a new object. The new object inherits the properties and methods from its constructor.

The this keyword of the constructor does not has a value.
The value of this would be the new object created when the function is invoked.

====================================call==================================

All Functions was methods:if a function is not a method of a JavaScript object, it's a function of the global object (see Previous chapter).

1. The JavaScript call () method.

2. The call () method with Arguments.

Output:

John Doe,oslo,norway

=======================apply () =================

Similar to the call () method.

The difference is that the parameters of call () are separated, and the argument to apply () is an array.

Person.fullName.call () became person.fullName.apply (person1,["Oslo", "Norway"]);

= = Instance Math.max (Arg1,arg2,arg ...)

Math.max (n/a); will return 3

However, the JavaScript array does not have the max () method, so you can apply the Math.max () method.

Math.max.apply (null, [I/n]); would also return 3

The first parameter (NULL) does not matter. In this example, it is not used.

[Javascript-function] Functions Invocation/call (function calls) and call () and apply () methods

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.