Skip to content

JavaScript Decorator Functions

brett hartshorn edited this page Aug 6, 2015 · 6 revisions

You can define your own custom decorator functions, and they work as they normally do in Python. New to decorators? See TheCodeShips post on it here.

The JavaScript backend also defines some special decorators you can use to trigger some options. The following special decorators are applied at transpile time, not runtime.

@redef

Marks a function as redefinable at runtime, even when the --release options is used. The special method redefine can then be used to redefine the function at runtime.

@redef
def myfunc():
  pass
def foo():
  print 'foo'
myfunc.redefine(foo)

The example above changes myfunc to use foo You can also pass a string to the redefine method, the string must be JavaScript, not Python (the transpiler is not available at runtime).

https://github.com/rusthon/Rusthon/blob/master/examples/javascript_redefine_function.md

@debugger

This decorator only has an effect when using NW.js to test your application. It is applied to the entry point function of your program, and it takes care of opening the Chrome DevTools window just before your function loads, then when you set debugger.breakpoints to true, your program will halt on any uncaught exceptions.

debugger.breakpoints = True

@debugger
def main():
  ...

For more info on debugging, see: https://github.com/rusthon/Rusthon/wiki/JavaScript-Debugger

@bind()

The @bind() decorator is used as a shortcut to assign a function to something. If you want to add a method to a class on an external library, you can do it like this:

@bind(THREE.Camera.prototype.mymethod)
def mymethod():
  pass

Above is the same thing as doing this:

def mymethod():
  pass
THREE.Camera.prototype.mymethod = mymethod

Sidebar

Clone this wiki locally