Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript function call

There are four kinds of JavaScript function is called.

Each different ways that this mode initialization.


this keyword

In general, in Javascript, this refers to the current object functions executed.

Note Note that this is a reserved keyword, you can not modify this value.

JavaScript function call

In the previous section we have learned how to create a function.

The function code is executed after the function is called.


As a function call

Examples

function myFunction (a, b) {
return a * b;
}
myFunction (10, 2); // myFunction (10, 2) returns 20

try it"

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

In HTML, the default global object is the HTML page itself, so the function is part of the HTML page.

In the browser page object browser window (window objects). Over function automatically becomes a function of window object.

myFunction () and window.myFunction () is the same:

Examples

function myFunction (a, b) {
return a * b;
}
window.myFunction (10, 2); // window.myFunction (10, 2) returns 20

try it"

Note This is a commonly used method invokes a JavaScript function, but it is not good programming practice global variables, methods or functions likely to cause naming conflicts bug.

Global Objects

When a function calls itself is not an object?, This value will become the global object.

In the web browser, the browser window is the global object (window objects).

The examples of this return value is the window object:

Examples

function myFunction () {
return this;
}
myFunction (); // Returns the window object

try it"

Note Function as a global object to call, this will be the value of the global object.
Use window object as a variable is likely to cause the program to crash.

As a function of the method call

In JavaScript you can use a function defined as an object method.

The following example creates an object (myObject), the object has two properties (firstName and lastName), and a method (fullName):

Examples

var myObject = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + "" + this.lastName;
}
}
myObject.fullName (); // returns "John Doe"

try it"

fullName method is a function. Function belong to the object. myObject is the owner of the function.

this object has a JavaScript code. Value myObject object instance of this.

The following test! FullName modify this method and return value:

Examples

var myObject = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this;
}
}
myObject.fullName (); // returns [object Object] (owner of an object)

try it"

Note Function as an object method call, so this will be the value of the object itself.

Use the constructor function calls

If the function is called before using the new keyword, the constructor is called.

It looks like to create a new function, but in fact JavaScript functions are objects re-created:

Examples

// Constructor:
function myFunction (arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}

// This creates a new object
var x = new myFunction ( "John", "Doe");
x.firstName; // returns "John"

try it"

Call the constructor to create a new object. New object inherits the properties and methods of the constructor.

Note Constructor this keyword does not have any value.
this value to create an instance of an object (new object) in a function call.

As a function of the method call function

In JavaScript, functions are objects. JavaScript function has its attributes and methods.

call () and apply () function is a predefined method. Both methods can be used to call a function, the first argument two methods must be the object itself.

Examples

function myFunction (a, b) {
return a * b;
}
myFunction.call (myObject, 10, 2); // returns 20

Examples

function myFunction (a, b) {
return a * b;
}
myArray = [10,2];
myFunction.apply (myObject, myArray); // returns 20

Both methods use the object itself as the first parameter. The difference is that the second argument: apply passed is an array of parameters, that is, a plurality of parameters combined into an array passed, and then call as a call parameter passing (from the beginning of the second argument).

In JavaScript strict mode (strict mode), the function call in the first argument will become this value, even if the parameter is not an object.

In the non-JavaScript strict mode (non-strict mode), if the value of the first parameter is null or undefined, it will use the global object instead.

Note This way you can set the value by call () or apply (), and calling as a new method of an object that already exists.