Last active
February 9, 2020 16:16
-
-
Save roryl/468f346631ec9afa3cf9 to your computer and use it in GitHub Desktop.
First Class Functions - Examples of passing classes around
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
public function init(){ | |
return this; | |
} | |
public function otherFunc(){ | |
echo("called!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
public function init(){ | |
var a = new component_a(); //instantiate a | |
this.otherFunc = a.otherFunc; //Copy otherFunc from a to b | |
this.otherFunc(); //Outputs "called!" | |
call(this.otherFunc); | |
return this; | |
} | |
private function call(required function func){ | |
echo(arguments.func()); //An example of passing a function, executes otherFunc, outputs "called!" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* My xUnit Test | |
*/ | |
component extends="testbox.system.BaseSpec"{ | |
/*********************************** LIFE CYCLE Methods ***********************************/ | |
// executes before all test cases | |
function beforeTests(){ | |
} | |
// executes after all test cases | |
function afterTests(){ | |
} | |
// executes before every test case | |
function setup( currentMethod ){ | |
} | |
// executes after every test case | |
function teardown( currentMethod ){ | |
} | |
/*********************************** TEST CASES BELOW ***********************************/ | |
// Remember that test cases MUST start or end with the keyword 'test' | |
function componentATest(){ | |
new component_a(); | |
} | |
function componentBTest(){ | |
new component_b(); | |
} | |
function componentBOutputTest(){ | |
savecontent variable="outputs"{ | |
new component_b(); | |
} | |
expect(outputs).toBe("called!called!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment