-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-function.js
More file actions
72 lines (53 loc) · 1.75 KB
/
example-function.js
File metadata and controls
72 lines (53 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function Person(name, lastname) {
this.name = name
this.lastname = lastname
this.getName = function() {
return this.name;
}
}
// Creando objetos del tipo Person
const luis = new Person('Luis', 'Lavayen')
const franco = new Person('Franco', 'Beltran')
// Agregando nuevos metodo a la clase con Prototype
Person.prototype.getFullname = function() {
return this.name + " " + this.lastname;
}
Person.prototype.setName = function(newName) {
this.name = newName;
}
// Print Luis & Franco
console.log('My name is: ' + luis.getFullname());
console.log('My name is: ' + franco.getFullname());
luis.setName('Miguel');
franco.setName('Bruno');
// Print Luis & Franco
console.log('My name is: ' + luis.getFullname());
console.log('My name is: ' + franco.getFullname());
// Creando nueva propiedad a Person
Person.prototype.setNewProperty = function(property) {
this.newProperty = property;
}
// luis.setNewProperty('Padel');
luis.setNewProperty('Padel');
franco.setNewProperty('Volley');
console.log('My new property is: ' + luis.newProperty);
console.log('My new property is: ' + franco.newProperty);
console.log('');
// console.log(luis);
/****** USANDO HERENCIA*******/
function Student(name, lastname, course, note) {
Person.call(this, name, lastname);
this.course = course;
this.note = note;
}
// This is handled always on this way
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;
// Create and print the student fullname
const student = new Student('Juan', 'Perez', 'Javascript', 10);
console.log(student.getFullname());
// Change student name and add new property 'handball'
student.setName('Ricardo');
student.setNewProperty('Handball');
// Print full student object
console.log(student);