Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. // cascade
  2. querySelector('#confirm') // Get an object.
  3. ..text = 'Confirm' // Use its members.
  4. ..classes.add('important')
  5. ..onClick.listen((e) => window.alert('Confirmed!'));
  6.  
  7.  
  8. class Point {
  9. num x, y;
  10.  
  11. // Syntactic sugar for setting x and y
  12. // before the constructor body runs.
  13. Point(this.x, this.y);
  14. }
  15.  
  16. class Point {
  17. num x, y;
  18.  
  19. Point(this.x, this.y);
  20.  
  21. // Named constructor
  22. Point.origin() {
  23. x = 0;
  24. y = 0;
  25. }
  26. }
  27.  
  28. class Person {
  29. String firstName;
  30.  
  31. Person.fromJson(Map data) {
  32. print('in Person');
  33. }
  34. }
  35.  
  36. class Employee extends Person {
  37. // Person does not have a default constructor;
  38. // you must call super.fromJson(data).
  39. Employee.fromJson(Map data) : super.fromJson(data) {
  40. print('in Employee');
  41. }
  42. }
  43.  
  44. import 'dart:math';
  45.  
  46. class Point {
  47. final num x;
  48. final num y;
  49. final num distanceFromOrigin;
  50.  
  51. Point(x, y)
  52. : x = x,
  53. y = y,
  54. distanceFromOrigin = sqrt(x * x + y * y);
  55. }
  56.  
  57.  
  58. // Factory
  59. class Logger {
  60. final String name;
  61. bool mute = false;
  62.  
  63. // _cache is library-private, thanks to
  64. // the _ in front of its name.
  65. static final Map<String, Logger> _cache =
  66. <String, Logger>{};
  67.  
  68. factory Logger(String name) {
  69. if (_cache.containsKey(name)) {
  70. return _cache[name];
  71. } else {
  72. final logger = Logger._internal(name);
  73. _cache[name] = logger;
  74. return logger;
  75. }
  76. }
  77.  
  78. Logger._internal(this.name);
  79.  
  80. void log(String msg) {
  81. if (!mute) print(msg);
  82. }
  83. }
  84.  
  85.  
  86. // Gettery settery
  87. class Rectangle {
  88. num left, top, width, height;
  89.  
  90. Rectangle(this.left, this.top, this.width, this.height);
  91.  
  92. // Define two calculated properties: right and bottom.
  93. num get right => left + width;
  94. set right(num value) => left = value - width;
  95. num get bottom => top + height;
  96. set bottom(num value) => top = value - height;
  97. }
  98.  
  99.  
  100. // A person. The implicit interface contains greet().
  101. class Person {
  102. // In the interface, but visible only in this library.
  103. final _name;
  104.  
  105. // Not in the interface, since this is a constructor.
  106. Person(this._name);
  107.  
  108. // In the interface.
  109. String greet(String who) => 'Hello, $who. I am $_name.';
  110. }
  111.  
  112. // An implementation of the Person interface.
  113. class Impostor implements Person {
  114. get _name => '';
  115.  
  116. String greet(String who) => 'Hi $who. Do you know who I am?';
  117. }
  118.  
  119. String greetBob(Person person) => person.greet('Bob');
  120.  
  121. void main() {
  122. print(greetBob(Person('Kathy')));
  123. print(greetBob(Impostor()));
  124. }
  125.  
  126.  
  127. // Mixins
  128. class Musician extends Performer with Musical {
  129. // ···
  130. }
  131.  
  132. class Maestro extends Person
  133. with Musical, Aggressive, Demented {
  134. Maestro(String maestroName) {
  135. name = maestroName;
  136. canConduct = true;
  137. }
  138. }
  139.  
  140. mixin Musical {
  141. bool canPlayPiano = false;
  142. bool canCompose = false;
  143. bool canConduct = false;
  144.  
  145. void entertainMe() {
  146. if (canPlayPiano) {
  147. print('Playing piano');
  148. } else if (canConduct) {
  149. print('Waving hands');
  150. } else {
  151. print('Humming to self');
  152. }
  153. }
  154. }
  155.  
  156. mixin MusicalPerformer on Musician {
  157. // ···
  158. }
  159.  
  160. abstract class Cache<T> {
  161. T getByKey(String key);
  162. void setByKey(String key, T value);
  163. }
  164.  
  165.  
  166. class Foo<T extends SomeBaseClass> {
  167. // Implementation goes here...
  168. String toString() => "Instance of 'Foo<$T>'";
  169. }
  170.  
  171. class Extender extends SomeBaseClass {...}
  172.  
  173.  
  174. // Import only foo.
  175. import 'package:lib1/lib1.dart' show foo;
  176.  
  177. // Import all names EXCEPT foo.
  178. import 'package:lib2/lib2.dart' hide foo;
  179.  
  180.  
  181. // dart2js only!
  182. import 'package:greetings/hello.dart' deferred as hello;
  183.  
  184.  
  185. Future greet() async {
  186. await hello.loadLibrary();
  187. hello.printGreeting();
  188. }
  189.  
  190.  
  191. // You can await a loop... streams???
  192. await for (varOrType identifier in expression) {
  193. // Executes each time the stream emits a value.
  194. }
  195.  
  196.  
  197. // Generators
  198. // iterable
  199. Iterable<int> naturalsTo(int n) sync* {
  200. int k = 0;
  201. while (k < n) yield k++;
  202. }
  203.  
  204. // Callable classes
  205. class WannabeFunction {
  206. call(String a, String b, String c) => '$a $b $c!';
  207. }
  208.  
  209. main() {
  210. var wf = new WannabeFunction();
  211. var out = wf("Hi","there,","gang");
  212. print('$out');
  213.  
  214. // stream which is async
  215. Stream<int> asynchronousNaturalsTo(int n) async* {
  216. int k = 0;
  217. while (k < n) yield k++;
  218. }
  219.  
  220. // Typedefs??
  221. typedef Compare = int Function(Object a, Object b);
  222.  
  223. class SortedCollection {
  224. Compare compare;
  225.  
  226. SortedCollection(this.compare);
  227. }
  228.  
  229. // Initial, broken implementation.
  230. int sort(Object a, Object b) => 0;
  231.  
  232. void main() {
  233. SortedCollection coll = SortedCollection(sort);
  234. assert(coll.compare is Function);
  235. assert(coll.compare is Compare);
  236. }
  237.  
  238. int sort(int a, int b) => a - b;
  239.  
  240. void main() {
  241. assert(sort is Compare<int>); // True!
  242. }
  243.  
  244.  
  245. // Annotations
  246. library todo;
  247.  
  248. class Todo {
  249. final String who;
  250. final String what;
  251.  
  252. const Todo(this.who, this.what);
  253. }
  254.  
  255. ///
  256. import 'todo.dart';
  257.  
  258. @Todo('seth', 'make this do something')
  259. void doSomething() {
  260. print('do something');
  261. }
  262.  
  263.  
  264.  
  265. // Even fucking HashCode
  266. class Person {
  267. final String firstName, lastName;
  268.  
  269. Person(this.firstName, this.lastName);
  270.  
  271. // Override hashCode using strategy from Effective Java,
  272. // Chapter 11.
  273. @override
  274. int get hashCode {
  275. int result = 17;
  276. result = 37 * result + firstName.hashCode;
  277. result = 37 * result + lastName.hashCode;
  278. return result;
  279. }
  280.  
  281. // You should generally implement operator == if you
  282. // override hashCode.
  283. @override
  284. bool operator ==(dynamic other) {
  285. if (other is! Person) return false;
  286. Person person = other;
  287. return (person.firstName == firstName &&
  288. person.lastName == lastName);
  289. }
  290. }
  291.  
  292. void main() {
  293. var p1 = Person('Bob', 'Smith');
  294. var p2 = Person('Bob', 'Smith');
  295. var p3 = 'not a person';
  296. assert(p1.hashCode == p2.hashCode);
  297. assert(p1 == p2);
  298. assert(p1 != p3);
  299. }
  300.  
  301.  
  302.  
  303. message.style
  304. ..fontWeight = 'bold'
  305. ..fontSize = '3em';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement