diff --git a/JSObject.php b/JSObject.php index 609ff55..63910c3 100644 --- a/JSObject.php +++ b/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 diff --git a/README.md b/README.md index 110be9e..9289ec8 100644 --- a/README.md +++ b/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([ diff --git a/tests.php b/tests.php index 04c0dc8..78aba93 100644 --- a/tests.php +++ b/tests.php @@ -88,5 +88,34 @@ 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); + } } \ No newline at end of file