I have a function that passes an array to another function as an argument, there will be multiple data types in this array but I want to know how to pass a function or a reference to a function so the other function can call it at any time.
ex.
function A:
add(new Array("hello", some function));
function B:
public function b(args:Array) {
var myString = args[0];
var myFunc = args[1];
}
Simply pass the function name as an argument, no, just like in AS2 or JavaScript?
function functionToPass()
{
}
function otherFunction( f:Function )
{
// passed-in function available here
f();
}
otherFunction( functionToPass );
This is very easy in ActionScript:
function someFunction(foo, bar) {
...
}
function a() {
b(["hello", someFunction]);
}
function b(args:Array) {
var myFunc:Function = args[1];
myFunc(123, "helloworld");
}
You can do the following:
add(["string", function():void
{
trace('Code...');
}]);
...or...
...
add(["string", someFunction]);
...
private function someFunction():void
{
trace('Code...');
}
Related
I have this viewmodel
var MyVM = function() {
var self = this;
// i wrote private function like this
function getNames() {
//some logic
}
//i want to call function inside this code
$("body").on("onTrigger", (evt, msg) => {
getNames();
});
}
but it doesnt call the function getNames(). How to call private function?
try to make changes in your code like this,
var names = {
getNames : function(){
//some logic
}}
then call the function this way:
names.getNames();
or create an object of MyVM and call like this:
MyVM names = new MyVM();
names.getNames();
I need to convert a function to an object. For example, when I need to use the variable called fn I want to be able to use it as a function fn() or as an object fn.json(). I have code to do it, but I think it's not correct.
package lib.smartic {
// import
import lib.smartic.smartic;
// constructor $
public var fn = function(s):smartic{
return new smartic(s);
};
Function.prototype.json = function (s) {
// call
};}
How can I apply the prototype to my variable fn, not just to the object class?
Actually, Function is an Object... and as said #The_asMan, you shouldn't use prototypes.
Simple example:
var smartic: Smartic = new Smartic("someValue");
trace(smartic.json());
And definition of your Smartic class, without prototypes:
public class Smartic {
private var _value:String;
public function Smartic(value:String) {
_value = value;
}
public function json():String {
return "Some json here";
}
}
I don't know what you want to do exactily but if you want smartic to be a function you can do something like this:
keep the code givent by Nicolas Siver and add this function:
public function smarticFunc(value:String):Smartic
{
return new Smartic(value);
}
so you can use smarticFunc as function or as Smartic as it returns a Smartic.
I know I can use the addEventListener method to handle one:
addEventListener(SFSEvent.CONNECTION, MyMethod)
as I would for handling a method in another class? Like...
addEventListener(SFSEvent.CONNECTION, Myclass.class)
or
addEventListener(SFSEvent.CONNECTION, MyClass.method)
You may pass another function handler to a class
For example
Class A {
public function A() {
addEventListener(SFSEvent.CONNECTION, MyMethod);
}
private function _handler:Function;
public function set handler(value:Function):void {
_handler = value;
}
private function MyMethod(e:SFSEvent):void {
if (_handler) {
_handler.apply(null, someParam);
}
}
}
Then pass the target handler to A instance
var a:A = new A();
var b:Myclass = new Myclass();
a.handler = b.someMethod;
If the function is a static function, You may just do it like this
addEventListener(SFSEvent.CONNECTION, SomeClass.aStaticFunction);
I have a public variable and I am trying to set it, then read it from a different function:
public var str:String;
public function DailyVerse()
{
function create() {
str = "hello";
}
function take() {
var message:String = str;
trace(message);
}
take();
}
My trace results says null. Why does it not give me "hello"?
I'm not sure why you have this set up this way.... if you want to get and set variable, you use the getter and setter syntax for flash.
private var myRestrictedString:String;
public function get DailyVerse():String {
if(myRestrictedString == undefined) {
//Not yet created
myRestrictedString = "Something";
}
return myRestrictedString;
}
public function set DaileyVerse(string:String):void {
myRestrictedString = string;
}
Now you can access this from outside of your class like so:
myClass.DailyVerse = "Test";
trace(myClass.DailyVerse); //Outputs "Test"
Let's say we have a number of ExternalInterface.addCallback functions, like so:
ExternalInterface.addCallback( 'foo', handler );
ExternalInterface.addCallback( 'bar', handler );
ExternalInterface.addCallback( 'foobar', handler );
In the function handler I'd like to find the method name called through the external interface, such as foo, bar or foobar; is there a way? Eg:
private function handler(...args):void
{
arguments.callee.arguments[ 0 ];
}
But I doubt that'll work
This should work. Just pass a parameter that identifies your function from the javascript, for example a string. So when you call the flash-function in your javascript code, pass a string, which you then parse in your handler function. For example:
actionscript:
ExternalInterface.addCallback( 'foo', handler );
private function handler(s:String):void
{
if(s == "foo") {
//foo specific code
}
}
javascript:
document.getElementById('flashObj').foo("foo");
Not exactly what you are asking for, but it will give you the info you need.
Just make the first parameter that you send the function name or in this case the object with funcname attribute so you can test if it exists.
JavaSCript
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
thisMovie("ExternalInterfaceExample").genericCallBack({ funcname:'foo' })
thisMovie("ExternalInterfaceExample").genericCallBack({ funcname:'poo' })
thisMovie("ExternalInterfaceExample").genericCallBack({ funcname:'moo' })
AS3
public function foo( ):void{
trace('in foo')
}
public function poo( ):void{
trace('in poo')
}
public function moo( ):void{
trace('in moo')
}
public function genericCallBack( o:Object ):void{
trace( 'calling '+o.funcname)
this[o.funcname]()
}
// add this somewhere in your init process
ExternalInterface.addCallback( 'genericCallBack', genericCallBack );