O'Reilly logo

JavaScript® Bible, Sixth Edition by Danny Goodman, Brendan Eich, Michael Morrison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 34. Functions and Custom Objects

IN THIS CHAPTER

  • Creating function blocks

  • Passing parameters to functions

  • Creating your own objects

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 programming (OOP) revolution, then now is your chance to join. JavaScript is surprisingly full-featured when it comes to supporting OOP 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 code that executes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required