This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Comést Savatino
Posted Sep 04 - Read on Facebook

I'm experimenting with creating a module.

I've seen example modules that only use exports.MyFunction()...
While others have extended the Layer class. What's best practice here? Also, how exactly does "options={}" work?


class Foo extends Bar
constructor: (options={}) ->
super options


Jordan Robert Dobson

Depends on if you just need a method externally or you are creating an object (class) or you have a collection of methods you'd like to collect and keep together class (class methods)

I'm sure there are plenty of other reasons.

Jordan Robert Dobson

The constructor is basically the initializer and what sets up your instance of that class. It's where you would put default settings... Like every Foo object is blue and has an opacity of .33

The options part of that is an object you can pass in and read inside your constructor. s
So say you want to allow users to set an opacity other than .33 you could read options.opacity inside your constructor and set it based on the value the user passed.

The user would pass it like so:

myFoo = new Foo opacity: .75

Jordan Robert Dobson

So in your example you're also subclassing Bar meaning you'll get all the properties and methods that Bar has... inside of Foo but you're now able to define additional things inside your Foo class. The reason you call "super options" is because you're still relying on the functionality of Bar and you still need to call the subclassed constructor (Bar, which is essentially super) so Bar can do its setup for Foo too with any of the options that were passed into Foo.

I hope that helps and makes sense. :)

Jordan Robert Dobson

My recommendation is to start with Methods and if you start needing a whole bunch of functionality bundled up together to be portable then consider a class.

Comést Savatino

Jordan, thanks alot man. Very helpful!

Also, when extending a Class, in the above example, does the Foo's constructor override Bar's constructor? Is there a way to append Bar's arguments—programmatically, without having to rewrite the parent class's constructor—or at that point, should you just create a new class without an extension?

class Bar
constructor: (oldArgs) ->

class Foo extends Bar
constructor: (oldArg + newArgs) ->
super @newArg

Jordan Robert Dobson

No it doesn't override Bars constructor. By saying "super options" you call Bar's constructor... Bar does it's setup work that you expect (you are extending it after all) and then you're free to manipulate things as you see fit in your Foo class.

So perhaps Bar sets all it's instances to red background, you can override the background after calling "super options" to change it to what you want. You'd start to do that on the next line immediately after "super options"...

Ultimately you can choose what to pass to super it doesn't have to be the options argument. You can modify it before hand or create something completely new.

The main rule is... Always call super if you're extending an object... Otherwise why extend it? (There's probably a reason but I've always been taught that you must)

Comést Savatino

Brilliant! Thanks :D

Jordan Robert Dobson

One last thing... A lot of times I do a lot of setup before I call super and pass things into it and then do more after I set it up... It just depends on what you're trying to solve.

Maybe you want to allow the user to set a custom text label, but you always want to have a fallback label if one isn't given... Which you pass to super. Or you want to allow users to customize things but always provide a fall back...

Read the entire post on Facebook