Helper function for terseness

This commit is contained in:
Timothy Warren 2012-05-25 08:33:16 -04:00
parent 27983ddcfe
commit 74b40fca1e
3 changed files with 62 additions and 0 deletions

View File

@ -11,6 +11,26 @@
*/
// --------------------------------------------------------------------------
// ! Shortcut Function
// --------------------------------------------------------------------------
/**
* Returns an instance of a JSObject instantiated with the passed parameters
*
* @param mixed
* @return JSObject
*/
function JSObject()
{
// Create the object
$obj = new JSObject();
// Set the parameters
call_user_func_array([$obj, '__construct'], func_get_args());
return $obj;
}
// -------------------------------------------------------------------------
/**
* JSObject class

View File

@ -36,6 +36,19 @@ The following array functions are blacklisted because of their parameter orderin
$obj->x(); // Returns 50
or, with the helper function:
$obj = JSObject([
'a' => 10,
'b' => 5,
'c' => 0,
'x' => function() {
return $this->a * $this->b;
}
]);
$obj->x(); // Returns 50
* Get an array of the properties of the object:
$obj = new JSObject([

View File

@ -89,4 +89,33 @@ class JSObjectTests extends UnitTestCase {
$this->assertEqual($obj->array_values(), ['foo','bar']);
}
function TestJSObjectFunc()
{
$obj1 = new JSObject([
'x' => 'foo',
'y' => 'bar'
]);
$obj2 = JSObject([
'x' => 'foo',
'y' => 'bar'
]);
$this->assertEqual($obj1, $obj2);
}
function TestFuncClosure()
{
$obj = JSObject([
'a' => 10,
'b' => 5,
'c' => 0,
'x' => function() {
return $this->a * $this->b;
}
]);
$this->assertEqual($obj->x(), 50);
}
}