Function.call

call

Description

Function.call(object this, [mixed arg, …])

Parameters

Name Description Type Default Optional
this object No
arg, … mixed Yes

Examples

Example #1 – call example
var f = function () { return this; }; console.log(f.call(location.hash)); // Sets a custom this value for the function.
Example #2 – call example
var getName = function () { return this.name.first + ' ' + this.name.last; } var lars = { name: { first: 'Lars', last: 'Doe' } }; console.log(getName.call(lars));
Example #3 – call example
var getName = function (lastFirst, includeAge) { var result = ''; if (lastFirst) { result = this.name.last + ', ' + this.name.first; } else { result = this.name.first + ' ' + this.name.last; } if (includeAge) { result += ' (' + this.age + ')'; } return result; } var lars = { name: { first: 'Lars', last: 'Doe' }, age: 20 }; console.log(getName.call(lars)); // Lars Doe console.log(getName.call(lars, true)); // Doe, Lars console.log(getName.call(lars, false, true)); // Lars Doe (20)

External references