Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Check if a context has a function property, optionally with specified number of formal parameters...

 

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
hasFunction( context, key[, paramCount] );

Parameters

ParameterTypeMandatoryNotesAPI Version
contextVariant(tick)The object to which the function is attached.1.5
keyString(tick)The name of the property that should be a function.1.5
paramCountNumber(error)

The number of formally defined parameters the function should have.

Default: Don't check parameter count.

Note: JS API functions don't specify any formal parameters (sad)

1.5

Return Values

ValueTypeNotesAPI Version
<error>ErrorMost likely the parameters are wrong.1.0
trueBooleanThe assertion passed.1.0
falseBooleanThe assertion failed.1.0

Notes

The assertion is performed in three stages:

  • Assert that the property 'key' exists
  • Assert that the value of the property is a function
  • If paramCount specified, assert the number of formal parameters matches paramCount

The message added to the test results indicates indicates which stage hasFunction( ) had got to when the assertion passed or failed.

Example

Code Block
themeRDark
languagejavascript
linenumberstrue
// let's say we're testing this...
var foo = {
	bar: function(a, b, c) {},
	chimp: "not a function"
}
 
Test("foo.bar() defined", Test.ANY( ), function() {
	hasFunction( foo, "meh" ); // fails - no property called "meh"
	hasFunction( foo, "bar" ); // passes
	hasFunction( foo, "chimp" ); // fails - chimp is not a function
	hasFunction( foo, "bar", 3 ); // passes
	hasFunction( foo, "bar", 4 ); // fails - bar() has 3 formal params
});
Div
classbox
Availability
Status
colour
Yellow
Green
title
Beta
Stable

Requires:

Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Div
classbox

Test API

Topics:

Child pages (Children Display)
alltrue
depthall
pageTest API
excerpttrue