how to make a class function with actionscript, I need to have a few static tool functions that are easily used from other classes like
test = tools.rtrim(xx);
e.g. this does not compile:
package com.my.tools
{
static function rtrim(string:String):String {
return string.replace(/\s+$/,"");
}
}
It needs to be attached to a class type not a package
Try
package com.my.tools
{
public class Tools
{
public static function rtrim(string:String):String
{
return string.replace(/\s+$/,"");
}
}
}
You can then use it through Tools.rtrim("yourString");
In case your collection of tools gets big it may be useful to use top-level functions as well.
Specially if you want to reuse a tiny selection of your "tools" in other projects without wasting file size by compiling the unused ones (which happens if you include them all in the same class).
In order to do so, in your package folder you will have to create one file per function. Each file should be named the same way as its related function.
e.g. the content of each file named rtrim.as would look like this :
package com.my.tools {
public function rtrim(str:String) : String {
return string.replace(/\s+$/,"");
}
}
Then you will just have to import the top-level function where you need it :
package my {
import com.my.tools.rtrim;
public class Test
{
rtrim("bla bla");
}
}
Related
I was wondering if there is any possibility to add some functions to the prototype of a class in Typescript.
My situation is as this:
I've got a file with a class, e.g. image.ts:
export class Image {
b64img: string;
}
This class is generated and so I can't add methods to it. I'm using this class in other generated classes, so creating a new class with extends Image is not possible for adding functions.
Is there any option that I can add a function to my class in a separated file so I could use it on all objects of the class Image?
Thanks
You can. You can extend it by defining an extension to the module it is defined in.
Create a file called image-extension.ts or something like that.
Now, if in that file you would import Image like this:
import { Image } from `image`;
declare module 'image' { // same name than in the import!
export interface Image {
newMethod: () => void;
}
}
Image.prototype.newMethod = function() {
console.log('new method');
}
Now, wherever you import this file, you can use that method:
import { Image } from 'image';
import 'image-extension'; // import just for side-effects
Of course, you can also add the declare module to a d.ts file, so it gets loaded automatically, and in another file add the true functions to the prototype, making sure that such a file gets loaded in your application
I would like to say that i am quite new to prestashop.
I have added FrontController.php to override/classes/controller.
The file contains the following code
class FrontControllerCore extends Controller
{
public function thefunct()
{
return 'AA';
}
}
Now in the header.tpl i try to call the function by using {FrontController::thefunct()}.
When I put the public function in classes/controller it works, but when I put it in the override folder it doesn't.
How do I display the function in my header.tpl?
And how does the override folder work then?
(I assume you use ps 1.5)
If you are overriding FrontController you need to initiate your class like so
class FrontController extends FrontControllerCore
and delete cache/class_index.php file, (I mean, delete it everytime you override new controller or class)
Teach yourself how to override in ps. through excellent docs.
EDIT:
class FrontController extends FrontControllerCore {
//here you should create and assign variables which you want to use in templates
public function initContent()
{
parent::initContent();
//in such a way you assign variables into smarty template
$this->context->smarty->assign(
array(
'acme_variable' => $this->acme()
)
);
}
protected function acme()
{
return "Wile E. Coyote and The Road Runner";
}
}
//header.tpl
<div>{$acme_variable}</div>
I was attempting to use this and this to figure out how to set up some math classes (the sin/cos functions I use for anything top down.) but I have it set to import package.class (the names of course.) but It said that "defintion package:Class not found". This confuses me and I am going to assume I have to do something with folders. However I'm not sure what folders to put it in/what to do to enter things specifically in a folder.
//import sthreets.CustomFuncs;
private function Movement()
{
if(LEFT==true)
{
rotation=rotation-6;
}
if(RIGHT==true)
{
rotation=rotation+6;
}
if(UP==true)
{
//x=x+CustomFuncs.TopDownMove("x", rotation, 0);
//y=y+CustomFuncs.TopDownMove("y", rotation, 0);
}
if(DOWN==true)
{
//x=x-CustomFuncs.TopDownMove("x", rotation, 0);
//y=y-CustomFuncs.TopDownMove("y", rotation, 0);
}
}
Commented out stuff was giving me errors, here's the CustomFuncs code.
package sthreets
{
public class CustomFuncs
{
public function CustomFuncs()
{
}
public function TopDownMove(xy:String, rot:Number, offSet:Number):Number
{
if(xy=="x")
{
return cos(DegreesToRadions(rot)+offSet)
}
if(xy=="y")
{
return sin(DegreesToRadions(rot)+offSet)
}
}
public function DegreesToRadions(rot:Number):Number
{
return rot*Math.PI/180;
}
}
}
Package names are determined by the location from src. src/my/package/name is my.package.name. The package you import at the top of any class must match the actual package, otherwise the compiler will not find the class.
So... say you have this structure in your folders:
src
->my
->package
->name
->ClassName
The class would be set up as so:
package my.package.name {
public class ClassName {
public function ClassName(){
//construct
}
}
}
And you would import it using:
import my.package.name.ClassName;
or
import my.package.name.*; //only use the wildcard if every class in that package is being used, otherwise you will include code you may or may not need to in your project
Hopefully that helps explain packages and imports a bit.
I also noticed that you are using the functions in your CustomFuncs class as if they were static (as you would with Math.cos() or similar). Your class is not set up to work this way. To do that, you need to use this static access modifier.
package sthreets
{
public class CustomFuncs
{
public static function TopDownMove(xy:String, rot:Number, offSet:Number):Number
{
if(xy=="x")
{
return cos(DegreesToRadions(rot)+offSet)
}
if(xy=="y")
{
return sin(DegreesToRadions(rot)+offSet)
}
}
public static function DegreesToRadions(rot:Number):Number
{
return rot*Math.PI/180;
}
}
}
The static access modifier means that the object (in this case, the functions) only exist once in any instance of the app, whereas a standard access modifier (public, private, protected, internal, mx_internal, final) create one object for every single instance of the class. Using the static access modifier allows you to access objects through ClassName.objectName because the objects belong to the class, not to the parent object. To get to those objects, you never have to instantiate (you will notice in the above code that I removed your constructor because it is unnecessary in this case).
Note: The following isn't necessarily directed at you, but I am including it for any future readers who might visit this question
This blog post gives a fairly good rundown of the standard access modifiers. The comparisons it makes in relation to beer are incredibly good and incredibly easy for someone with limited knowledge about the modifiers to understand.
I also suggest reading this Wikipedia article on "Static Variable" to help you understand what a static object is and where and how to use it.
AS3 strictly follows OOP (object oriented programming) and ECMAScript policies and its syntax is based heavily on Java. For these reasons, nearly every principle used by other OOP or ECMA languages applies to AS3 as well. This means that you can improve your AS3 skills by reading up on these principles, even if they are not specifically for AS3 (AS3 OOP tutorials are fairly limited in both quantity and quality so it can be difficult for someone who is learning AS3 as their first OOP language to learn these principles).
I am doing an Actionscript 3.0 project which involves introspection. I am wondering if there is a way to get all the classes within a given package structure.
For e.g. Say there are three as3 classes:
com.example.test.classOne
com.example.test.classTwo
com.example.test.classThree
I want to be able to say
getClassesUnderPackageName("com.example.test");
and get back
"com.example.test::classOne"
"com.example.test::classTwo"
"com.example.test::classThree".
Is there a way to do that?
If this is not possible, is there a way to read classes which have the same metadata?
E.g. If all the mentioned classes have the same metadata [MetadataName(type="example")] defined, is there a way to say
getClassesWithSameMetadata("MetadataName");
and get back
"com.example.test::classOne"
"com.example.test::classTwo"
"com.example.test::classThree".
Thank you.
you can use flash.utils.describeType to return XML data containing this information. it works differently on base classes, like flash.display.Sprite, but for custom classes/directories, you can write something like this:
package branchA.branchB.branchC
{
//Imports
import flash.utils.describeType;
//Class
public class Test
{
//Constructor
public function Test()
{
trace(describeType(this).#name);
}
}
}
//OUTPUT: branchA.branchB.branchC::Test
if you wanted to return the base class, you could write something like this:
package
{
//Imports
import flash.display.Sprite;
import flash.utils.describeType;
//Class
public class Test extends Sprite
{
//Constructor
public function Test()
{
trace(describeType(this).#base);
}
}
}
//OUTPUT: flash.display::Sprite
there is lots of other useful information you can get by parsing the returned XML data of describeType.
Update:
class objects do not need to have been instantiated first in order to retrieve their information via describeType(). you could build a public static function (or whatever) which accepts an array of your class objects and returns an array of strings containing the required describeType data.
something like this:
package
{
import flash.utils.describeType;
final public class Describe
{
public static function packageNames(classObjects:Array):Vector.<String>
{
var names:Vector.<String> = new Vector.<String>();
for each (var classObject in classObjects)
names.push(describeType(classObject.#name.toString()));
return names;
}
}
}
then from anywhere in your program, you can pass an array of all the classes like this:
var names:Vector.<String> = Describe.packageNames(new Array(classOne, classTwo, classThree));
trace(names);
//Output:
//com.example.test::classOne
//com.example.test::classTwo
//com.example.test::classThree
There's no inbuilt mechanism for finding classes without already knowing the class name. :(
However if you load in a SWF as a ByteArray then it's possible to iterate through the classes in it.
This might be overkill for what you want.
http://www.bytearray.org/?p=175
Take a look at AS3 Commons Bytecode. It allows you to do Bytecode based reflection. You can list all classes (you'll need to filter those if you just want a particular package), list classes with certain metadata or classes that implement a certain interface.
I would like to modify an Accordion class to suit my needs.
Instead of simply extending Accordion, I would like to copy and paste the whole class as a start, with the new class name "MyAccordion", into the src folder; to gain the maximum freedom(I assume).
However, several problems encountered. For the "include "../core/Version.as";" error, I had solved by replacing it with a explicit Version static const string. But for the problems lead by the inheritance, e.g. AccordionHeader, etc, I found that there would be too many files to be edited when going down the stream. I suspect I mis-understand the whole logic of editing the class.
Would anyone give me some help? May be some reference for me to read, or even just some keywords for me to search. Thanks in advance.
Well - for all the reasons your discovering, you actually don't have flexibility when leveraging "boilerplate" code like this. Use Extend and Override to properly modify existing classes:
package com.yourSite.src
{
public class Foo
{
public function Foo
{
}
public function foo():void
{
trace("foo");
}
}
}
package com.yourSite.src
{
public class Bar extends Foo
{
public function Bar
{
}
override public function foo():void
{
trace("bar");
}
}
}
So, write a class that Extends Accordion, and override anything that you want to work differently. Any other functionality you may need can be added as required. OOP 101 :D
Check out the link above for a more cohesive discussion.
Cheers!