Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Dart programming language have an equivalent to Javascript's "prototype"?

Tags:

dart

In Dart, is it possible for a function to have a prototype associated with it?

Example Javascript code:

doStuff.prototype.isDefined = true; //is there anything like Javascript's function prototypes in Dart?
function doStuff(){
    console.log("The function doStuff was called!");
}

Is it possible to do the equivalent of this in Dart (i.e., create a list of properties for each function?)

like image 830
Anderson Green Avatar asked Oct 01 '12 02:10

Anderson Green


People also ask

What language is dart similar to?

Dart is similar to C# and Java in syntax, so it's quick to learn.

Is Dart better than JavaScript?

Speed. JavaScript is an interpreted language, so it might feel lighter and faster. It's faster than other compiled languages like Java. However, Dart proved to be much faster when benchmarked against JavaScript.

Is Dart a good language?

Dart is an open source, purely object-oriented, optionally typed, and a class-based language which has excellent support for functional as well as reactive programming. Unlike C# or Java, Dart is not bloated at all. In fact, it's a relatively simple, modern and highly efficient language to work with.


2 Answers

Two things to address here:

First, Dart doesn't have prototypes or prototypal inheritance, and instead uses classical inheritance. Rather than a prototype, objects have a class, and instead of a prototype chain, objects have an super classes.

Second, for your specific case, I think we'd have to see more of what you need to do to figure out the idiomatic way to do it in Dart. It should soon be possible to emulate functions with objects so that you an invoke an object and still have state and other methods associated with it.

See this article for more: http://www.dartlang.org/articles/emulating-functions/

When that capability lands you'll be able to do this:

class DoStuff {
  bool isDefined = true;
  call() => print("The function doStuff was called!");
}
var doStuff = new DoStuff();

main() => doStuff();

Which works if you have a fixed set of metadata about your function that you need to keep track of. It's slightly different from the JavaScript because each instance of the function is Dart will have it's own state for isDefined. I'm not sure if it's possible or easy to get multiple instances of the function is JavasScript, but you might need to make isDefined static so that the value is shared across all instances.

like image 76
Justin Fagnani Avatar answered Sep 21 '22 18:09

Justin Fagnani


Dart does not allow you to add or remove member variables from an instance of a class at runtime. Rewriting your example in Dart it might look something like this:

class doStuff {
  bool isDefined;
  doStuff() {
    isDefined = true;
  }
  void stuff() {
    print('The function stuff was called!');
  }
}

main() {
  new doStuff().stuff();
}

If you wanted to add a property bag to a class in Dart you would write:

class PropertyObject {
  Map<String, Dynamic> properties;

  PropertyObject() {
    properties = new Map<String, Dynamic>();
  }

  Dynamic operator[](String K) => properties[K];
  void operator[]=(String K, Dynamic V) => properties[K] = V;
}

main() {
  PropertyObject bag = new PropertyObject();
  bag['foo'] = 'world';
  print('Hello ${bag['foo']}');
}

Note that you can't access map properties using the '.' operator.

like image 31
Cutch Avatar answered Sep 22 '22 18:09

Cutch