Helper function for terseness
This commit is contained in:
parent
27983ddcfe
commit
74b40fca1e
20
JSObject.php
20
JSObject.php
@ -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
|
||||
|
13
README.md
13
README.md
@ -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([
|
||||
|
29
tests.php
29
tests.php
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user