Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------
- //Przykład konstruktora i tworzenia obiektów z jego użyciem:
- function Zwierze (gatunek,liczbaNog,odglos) {
- this.gatunek = gatunek;
- this.liczbaNog = liczbaNog;
- this.odglos = odglos;
- }
- var pies = new Zwierze("pies",4,"hau hau");
- var kura = new Zwierze("kura",4,"ko ko ko");
- console.log(pies);
- console.log(kura);
- console.log("\n");
- //-----------------------------------------------------------
- //Przykład użycia prototypu - defnicja metody dajGlos() dla obiektów tworzonych z użyciem konstruktora Zwierze:
- Zwierze.prototype.dajGlos = function() {
- var msg = this.gatunek + " mowi " + this.odglos;
- console.log(msg);
- }
- pies.dajGlos();
- kura.dajGlos();
- //-----------------------------------------------------------
- //zad 1
- function Student (imie,nazwisko,oceny){
- this.imie = imie;
- this.nazwisko = nazwisko;
- this.oceny = [];
- }
- Student.prototype.dodajOcene = function(ocena) {
- this.oceny+=ocena;
- console.log(this.oceny);
- }
- Student.prototype.sredniaOcen = function() {
- var suma = 0;
- this.oceny.forEach(function(){
- suma+=ocena;
- return suma;
- })
- var srednia = suma/this.oceny.length;
- console.log(srednia);
- }
- var student1 = new Student("Adrian","Dziuniak");
- student1.dodajOcene([4,3,3]);
- student1.sredniaOcen();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement