Chapter 23. Function Objects and Custom Objects

IN THIS CHAPTER

  • Creating function blocks

  • Passing parameters to functions

  • Creating your own objects

  • The Object object

By now, you've seen dozens of JavaScript functions in action and probably have a pretty good feel for the way they work. This chapter provides the Function object specification and delves into the fun prospect of creating objects in your JavaScript code. If you've missed out on the object-oriented (OO) programming revolution, now is your chance to join. JavaScript is surprisingly full-featured when it comes to supporting OO and allowing you to develop scripts that rely heavily on custom objects.

Function Object

Properties

Methods

Event Handlers

arguments

apply()

 

arity

call()

 

caller

toString()

 

constructor

valueOf()

 

length

  

prototype

  

Syntax

Creating a Function object:

function functionName([arg1,...[,argN]])
{
   statement(s)
}
var funcName = new Function(["argName1",...[,"argNameN"],
          "statement1;...[;statementN]"])object.eventHandlerName =
          function([arg1,...[,argN]]) {statement(s)}

Accessing Function object properties and methods:

functionObject.property | method([parameters])

Compatibility: WinIE3+, MacIE3+, NN2+, Moz+, Safari+

About this object

JavaScript accommodates what other languages might call procedures, subroutines, and functions all in one type of structure: the custom function. A function may return a value (if programmed to do so with the return keyword), but it does not have to return any value. With the exception of JavaScript ...

Get JavaScript® Bible, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.