Hi, Let Us Study About JavaScript(II)

Calling JavaScript functions

There is the case of how to call the function in JS below,check here

Function myFunction (a, b){

Return a * b;

}

MyFunction (10, 2); //myFunction (10, 2) returns 20

The above functions do not belong to any object. But it is always the default global object in JavaScript. 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 object. The above functions will automatically become functions of windows objects. MyFunction() is the same as window. myFunction(). Here is the example below to tell you what window.myFunction() to do,check here

There is the case of how to call the function in JS below,

Function myFunction (a, b){

Return a * b;

}

window.MyFunction (10, 2); //myFunction (10, 2) returns 20

Functions are called as methods

In JavaScript, you can define functions as methods of objects. check here The following example creates an object (myObject), which has two attributes (firstName and lastName) and a method (fullName):

var myObject = {

firstName:”John”,

lastName: “Doe”,

fullName: function () {

return this.firstName + ” ” + this.lastName;

}

}

myObject.fullName(); // Return “John Doe”

The fullName method is a function. Functions belong to objects. MyObject is the owner of a function.

Leave a Reply

Your email address will not be published. Required fields are marked *