Advertisement
Guest User

index

a guest
Mar 30th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. var counter = 0;
  2.  
  3. function Ant(name) {
  4. this.name = name;
  5. this.id = ++counter;
  6. }
  7.  
  8. Ant.meets = {}
  9.  
  10. Ant.prototype.hello = function (other) {
  11. if (!Ant.meets[this.id]) {
  12. Ant.meets[this.id] = []
  13. }
  14. Ant.meets[this.id].push({name: other.name, date: new Date()});
  15. };
  16.  
  17. Ant.prototype.about = function () {
  18. var name = this.name;
  19. var meets = Ant.meets[this.id].map(function (meet) {
  20. return 'My name is ' + name + '. I met ' + meet.name + ' at ' + meet.date;
  21. });
  22.  
  23. return meets.join('\n');
  24. }
  25.  
  26. var ant1 = new Ant('ant1');
  27. var ant2 = new Ant('ant2');
  28. var ant3 = new Ant('ant3');
  29.  
  30. ant1.hello(ant3); // встреча
  31. setTimeout(function(){
  32. ant2.hello(ant3); // встреча
  33. setTimeout(function(){
  34. ant3.hello(ant1); // встреча
  35.  
  36. console.log('ant1',ant1.about()); // просим рассказать о себе и встречах
  37. console.log('ant2',ant2.about()); // просим рассказать о себе и встречах
  38. console.log('ant3',ant3.about()); // просим рассказать о себе и встречах
  39.  
  40. },2000);
  41. },2000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement