Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. //-----------------------------------------------------------
  2. //Przykład konstruktora i tworzenia obiektów z jego użyciem:
  3.  
  4. function Zwierze (gatunek,liczbaNog,odglos) {
  5.  
  6. this.gatunek = gatunek;
  7. this.liczbaNog = liczbaNog;
  8. this.odglos = odglos;
  9. }
  10.  
  11. var pies = new Zwierze("pies",4,"hau hau");
  12. var kura = new Zwierze("kura",4,"ko ko ko");
  13.  
  14. console.log(pies);
  15. console.log(kura);
  16.  
  17. console.log("\n");
  18.  
  19.  
  20. //-----------------------------------------------------------
  21. //Przykład użycia prototypu - defnicja metody dajGlos() dla obiektów tworzonych z użyciem konstruktora Zwierze:
  22.  
  23. Zwierze.prototype.dajGlos = function() {
  24.  
  25. var msg = this.gatunek + " mowi " + this.odglos;
  26. console.log(msg);
  27. }
  28.  
  29. pies.dajGlos();
  30. kura.dajGlos();
  31.  
  32.  
  33.  
  34. //-----------------------------------------------------------
  35. //zad 1
  36.  
  37.  
  38. function Student (imie,nazwisko,oceny){
  39.  
  40. this.imie = imie;
  41. this.nazwisko = nazwisko;
  42. this.oceny = [];
  43. }
  44.  
  45. Student.prototype.dodajOcene = function(ocena) {
  46.  
  47. this.oceny+=ocena;
  48. console.log(this.oceny);
  49. }
  50.  
  51. Student.prototype.sredniaOcen = function() {
  52.  
  53. var suma = 0;
  54. this.oceny.forEach(function(){
  55. suma+=ocena;
  56. return suma;
  57.  
  58. })
  59.  
  60. var srednia = suma/this.oceny.length;
  61. console.log(srednia);
  62. }
  63.  
  64.  
  65.  
  66.  
  67. var student1 = new Student("Adrian","Dziuniak");
  68. student1.dodajOcene([4,3,3]);
  69.  
  70. student1.sredniaOcen();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement