JavaScript function calls

Source: Internet
Author: User
Tags object object

Tag: The His str window script must have the keyword function defined nbsp define

1.JavaScript function Call

There are 4 ways to call JavaScript functions.

The difference between each method is the initialization of this.

1.1 this keyword

In general, in JavaScript, this points to the current object when the function executes.

Note This is a reserved keyword and you cannot modify the value of this.

1.2 Calling JavaScript functions

The code in the function executes after the function is called.

1.3 As a function call

function MyFunction (A, b) {    return A * b;} MyFunction (2);           // myFunction (10, 2) return

The above function does not belong to any object. In JavaScript, however, it is always the default global object.

The default global object in HTML is the HTML page itself, so the function belongs to the HTML page.

The Page object in the browser is the browser window (the Window object). The function above will automatically become a function of the Window object.

MyFunction () and window.myfunction () are the same:

1.4 Global Objects

When a function is not called by its own object, the value of this will become a global object.

In a Web browser, the global object is a browser window (the Window object).

The value returned by this instance is the Window object:

function MyFunction () {    returnthis;} MyFunction ();                 // returns the Window object

1.5 functions as Method calls

In JavaScript, you can define a function as an object's method.

The following instance creates an object (MyObject) with two properties (firstName and lastName), and one method (fullName):

var myObject = {    firstName:"John",    "  Doe",    fullname:function () {        returnthis" "  This . lastName;    }} Myobject.fullname ();          // return to "John Doe"

The fullName method is a function. The function belongs to the object. MyObject is the owner of the function.

This object, which has JavaScript code. The value of this in the instance is the myObject object.

Test the following! Modify the FullName method and return this value:

var myObject = {    firstName:"John",    "  Doe",    fullname:function () {        return this ;    }} Myobject.fullname ();           // return [Object Object] (owner objects)

1.6 Calling a function using the constructor function

If the new keyword is used before a function call, the constructor is called.

This looks like the creation of a new function, but the JavaScript function is actually a re-created object:

 //  constructor:   function MyFunction (arg1, arg2) { this . FirstName = Arg1;  this . LastName = arg2;}  //  This creates a new object  var  x = new  myFunction ("  john   ", "  doe   "  //  return "John"  

The call to the constructor creates a new object. The new object inherits the properties and methods of the constructor.

1.7 Calling functions as function methods

In JavaScript, a function is an object. The JavaScript function has its properties and methods.

Call () and apply () are predefined function methods. Two methods can be used to call a function, and the first parameter of two methods must be the object itself.

function MyFunction (A, b) {    return A *2);     // return
function MyFunction (A, b) {    return A *= [2= Myfunction.apply (MyObject, myArray);  // return

All two methods use the object itself as the first parameter. The difference between the two is the second argument: Apply passes in an array of parameters, which is the combination of multiple parameters into an array, and call is passed in as a call parameter (starting with the second argument).

Under JavaScript Strict mode (strict mode), when a function is called, the first parameter becomes the value of this, even if the parameter is not an object.

In JavaScript's non-strict mode, if the value of the first parameter is null or undefined, it uses the global object override. non-strict

You can set the value of this with the call () or the Apply () method, and it is called as a new method of an existing object.

JavaScript function calls

Related Article

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.