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 |
|---|---|---|
|
| |
|
| |
|
| |
|
| |
| ||
|
Syntax
Creating a function object:
functionfunctionName([arg1,...[,argN]]) {statement(s)}
varfuncName= 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access