JavaScript: Function.prototype

By Xah Lee. Date: . Last updated: .

Function.prototype is the value of the property key "prototype" of the Function object.

〔➤see JavaScript: Function Object

console.log( Function.hasOwnProperty ( "prototype" ) ); // true

Type of Function.prototype is function object.

〔➤see JavaScript: Data Types

ECMAScript 2015 §Fundamental Objects#sec-properties-of-the-function-prototype-object

// type of Function.prototype
console.log ( typeof Function.prototype === "function" ); // true
console.log ( Object.prototype.toString.call( Function.prototype ) === "[object Function]" ) // true

Function.prototype(…) is not useful. You can call Function.prototype(…). It takes any number of arguments, and always return undefined.

// Function.prototype() takes any arg, returns undefined
console.log ( Function.prototype() );     // undefined
console.log ( Function.prototype(3) );    // undefined
console.log ( Function.prototype(3, 4) ); // undefined
console.log ( Function.prototype({}) ); // undefined

Parent of Function.prototype is Object.prototype.

// parent of Function.prototype
console.log ( Object.getPrototypeOf ( Function.prototype ) === Object.prototype ); // true

Function.prototype is the parent of all function objects.

console.log ( Object.getPrototypeOf ( function f () {} ) === Function.prototype ); // true
console.log ( Object.getPrototypeOf ( (x => 3) ) === Function.prototype ); // true

〔➤see JavaScript: Prototype and Inheritance

Properties


Function.prototype.constructor

Function.prototype.constructor

Value is the Function function object.

function f1 () { }
console.log ( f1.constructor === Function ); // true

const f2 = (x => x+1);
console.log ( f2.constructor === Function ); // true

〔➤see JavaScript: Function Object

〔➤see JavaScript: Arrow Function

〔➤see JavaScript: Property Key "constructor"

ECMAScript 2015 §Fundamental Objects#sec-function.prototype.constructor

Function.prototype.call

Function.prototype.call ( thisArg, …args )

〔➤see JavaScript: Function Call, Apply, Bind

Function.prototype.apply

Function.prototype.apply ( thisArg, argArray )

〔➤see JavaScript: Function Call, Apply, Bind

Function.prototype.bind

Function.prototype.bind ( thisArg, …args )

〔➤see JavaScript: Function Call, Apply, Bind

Function.prototype.toString

Function.prototype.toString ( ) → Returns a string representation of the function. The returned result may not be exactly the same between browsers or other JavaScript implementation.

function f () { return 3; }
console.log ( f.toString () === "function f() { return 3; }" ); // true
const ff = (x => 3);
console.log ( ff.toString () === "x => 3" ); // true
console.log ( Array.toString () );
// function Array() { [native code] }

console.log ( Function.toString () );
// function Function() { [native code] }

console.log ( Object.toString () );
// function Object() { [native code] }

console.log ( Array.prototype.pop.toString () );
// function pop() { [native code] }

ECMAScript 2015 §Fundamental Objects#sec-function.prototype.tostring

Function.prototype[Symbol.hasInstance]

Function.prototype[Symbol.hasInstance] ( obj )

Returns true if the value of this.prototype is in the prototype chain of obj. Else, false.

〔➤see JavaScript: Keyword “this”

The following are equivalent:

Example of buildin function.

const ar = [3,4];

console.log ( Array[ Symbol.hasInstance ](ar) ); // true
console.log ( ar instanceof Array ); // true
console.log ( Array.prototype.isPrototypeOf ( ar ) ); // true

Example of user-defined function.

function F () { }

let ff = new F;

console.log ( F[ Symbol.hasInstance ](ff) ); // true
console.log ( ff instanceof F ); // true
console.log ( F.prototype.isPrototypeOf ( ff ) ); // true

〔➤see JavaScript “instanceof” Operator

〔➤see JavaScript: Symbol Tutorial

ECMAScript 2015 §Fundamental Objects#sec-function.prototype-@@hasinstance

Reference

ECMAScript 2015 §Fundamental Objects#sec-properties-of-the-function-prototype-object

Function Topic

  1. JavaScript: Define Function
  2. JavaScript: Function Optional Parameters
  3. JavaScript: Variable/Function Declaration Order, Name Hoisting
  4. JavaScript Variable Scope: Function Level vs Block Level
  5. JavaScript: Function Declaration vs Function Expression
  6. JavaScript: Closure
  7. JavaScript: Functional Programing
  8. JavaScript: Function Call, Apply, Bind
  9. JavaScript: Function Argument Default Value
  10. JavaScript: Function Rest Parameters
  11. JavaScript: Arrow Function

  1. JavaScript: Function Object
  2. JavaScript: Function.prototype
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.