2

I'd like to port this javascript code to dart.

 function Beagle() {
    this.argv_ = null;
    this.io = null;
  };
  Beagle.prototype.run = function() {
    this.io = this.argv_.io.push();
  };

 runCommandClass(Beagle);

the probleme is How to create object Beagle

How to create prototype object Beagle.prototype.run

3

2 Answers 2

4

This kind of Js code (function definition and prototype changes) can be ported to a Dart class. You can follow these main rules :

  • function Xxxx(){/* js code to init */} (pseudo Js class) translates to :
class Xxxx {
  /// constructor
  Xxxx() {
    /* Dart code to init */
  }
}
  • when you have contructor parameters like in function Xxxx(param1, param2){/* js code to init */} you have to create an other constructor with those parameters :
class Xxxx {
  /// constructor with parameters
  Xxxx(param1, param2) {
    /* Dart code to init with param1, param2 */
  }
}
  • Xxxx.prototype.method1 = function(p1, p2, p3){/* js code for method */} are like methods that have to be translated to :
class Xxxx {
  // .... other code

  /// method
  method1(p1, p2, p3) {
    /* Dart code */
  }
}

To make your code more clear you can also add type annotations on methods and constructors. This is recommanded by the Dart Style Guide.

Type annotations are important documentation for how a library should be used. Annotating the parameter and return types of public methods and functions helps users understand what the API expects and what it provides.

For instance :

class Xxxx {
  /// constructor
  Xxxx(String param1, int param2) {
    /* Dart code to init with param1, param2 */
  }

  /// method
  void method1(num p1, String p2, DateTime p3) {
    /* Dart code */
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If I use pure dart class, I cannot pass that class to runCommandClass(Beagle);. Because Beagle is dart class. not a javascript function
2
class Beagle { // 
  Map argv_;
  int io;
  Map portInfo;

  // could make sense to make this a constructor, that depends how the Terminal class uses it (didn't look)
  void run(this.argv_) { 
    this.portInfo_ = JSON.parse(this.argv_['argString']); // not tested
    io = argv_['io'].length;
  }

  void sendString_(String s) { // no idea what the underlines at the end of argv_, sendString_, ... are for
    // ...
  }

  void onRead_(String s) {}

  void onTerminalResize_(int width, int height) {}

  void exit(code) {
    // ...
  }

  void close() {
    // ...
  }
}

var b = new Beagle(); // not translated from the JS source - just added to show how to create a new object from the Beagle class
b.run(argvFromSomewhere);

This includes a some guessing about what the intention of the JavaScript code might be.

I prefer using specific types when porting from JavaScript. It helped me a lot finding bugs and understanding the intention. When I guessed the wrong type I get an exception at runtime, then I can reason about why I got an unexpected type and which of my assumptions were wrong.

1 Comment

I could solve my problem with this answer. Thanks Gunter as always!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.