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

 

Syntax

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

// 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
});
Availability

Requires:

Contents

Jump to:

Test API

Topics: