├── .gitignore ├── .replit ├── README.md ├── encapsulation ├── README.md ├── closure.js └── public-private.js ├── index.js ├── objects-introduction ├── class │ ├── class_hoisted.js │ ├── class_variable.js │ ├── override_constructor.js │ ├── sintactic_pov.js │ ├── sintactic_sugar.js │ └── static_method.js ├── contructor │ ├── constructor.js │ ├── constructor_parameters.js │ ├── new.js │ ├── object_constructor.js │ └── prototype.js ├── literals.js ├── methods.js ├── properties.js └── prototypes │ ├── extend_builtin.js │ └── prototype.js ├── oop-principles ├── Aggregation.js ├── Encapsulation.js ├── README.md ├── association.js ├── composition.js ├── inheritance.js ├── inheritance │ ├── inheritance_prototype.js │ └── object_create.js └── polymorphism.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *.pdf 2 | 3 | *.out -------------------------------------------------------------------------------- /.replit: -------------------------------------------------------------------------------- 1 | language = "nodejs" 2 | run = "npm start" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Javascript OOP 2 | 3 | This repo is a bunch of examples about Object Orientend programming in Javascript 4 | 5 | ### Requirements 6 | 7 | In order to understand the example you need: 8 | 9 | - Basic Knowledge of Javascript Syntax (functions, modules, objects, arrays, etc) 10 | 11 | ### Index 12 | 13 | - objects-introduction 14 | - literals.js 15 | - properties.js 16 | - oop principles 17 | 18 | -------------------------------------------------------------------------------- /encapsulation/README.md: -------------------------------------------------------------------------------- 1 | 2 | * public-private 3 | * closure -------------------------------------------------------------------------------- /encapsulation/closure.js: -------------------------------------------------------------------------------- 1 | // convention-base approach allows to access the property anyway 2 | // so to fix this approach is not using properties for internal members 3 | // but variables in the constructor 4 | 5 | function TheatreSeats() { 6 | const seats = []; 7 | 8 | this.placePerson = function (person) { 9 | seats.push(person); 10 | }; 11 | } 12 | 13 | // Scope and closure 14 | // const greeting = "Hello world"; 15 | 16 | // function greets(person) { 17 | // const fullName = person.name + " " + person.surname; 18 | 19 | // function displayGreeting() { 20 | // console.log(greeting + " " + fullName); 21 | // } 22 | 23 | // displayGreeting(); 24 | // } 25 | 26 | // greets({ name: "Ryan", surname: "Ray" }); 27 | 28 | // In javascript we can acces the outer scope even when the function that created 29 | // has terminated 30 | const greeting = "Good Morning"; 31 | let displayGreeting; 32 | 33 | function greets(person) { 34 | const fullName = person.name + " " + person.surname; 35 | 36 | return function () { 37 | console.log(greeting + " " + fullName); 38 | }; 39 | } 40 | 41 | displayGreeting = greets({ name: "Ryan", surname: "Ray" }); 42 | displayGreeting(); 43 | -------------------------------------------------------------------------------- /encapsulation/public-private.js: -------------------------------------------------------------------------------- 1 | // all properties and methods are public accesible 2 | // so there are some approach to try to solve this issue 3 | 4 | // convetion base approach 5 | 6 | // we can use underscore symbol for internal members 7 | // this is just a convetion, technicaly we can access to properties anyway 8 | 9 | function TheatreSeats() { 10 | this._seats = []; 11 | } 12 | 13 | TheatreSeats.prototype.placePerson = function (person) { 14 | this._seats.push(person); 15 | }; 16 | 17 | const theatreSeats = new TheatreSeats(); 18 | theatreSeats.placePerson({ name: "Ryan", surname: "Ray" }); 19 | 20 | theatreSeats; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import './objects-introduction/methods.js' 2 | // Write your code here 3 | 4 | console.log("Javascript OOP") -------------------------------------------------------------------------------- /objects-introduction/class/class_hoisted.js: -------------------------------------------------------------------------------- 1 | // unlike function class are not "hoisted" 2 | // it means, that we cannot use before its declaration 3 | 4 | // const car = new Car(); // error 5 | 6 | class Car { 7 | constructor() {} 8 | } -------------------------------------------------------------------------------- /objects-introduction/class/class_variable.js: -------------------------------------------------------------------------------- 1 | const Person = class { 2 | constructor(name, lastname) { 3 | this.name = name 4 | this.lastname = lastname 5 | } 6 | } 7 | 8 | const person = new Person("joe", "doe") -------------------------------------------------------------------------------- /objects-introduction/class/override_constructor.js: -------------------------------------------------------------------------------- 1 | // but is possible to override the default construtor behavior 2 | class MyClassTwo { 3 | constructor(value1, value2) { 4 | return { 5 | property1: value1, 6 | property2: value2, 7 | location: "London", 8 | }; 9 | } 10 | } 11 | 12 | let myClassTwo = new MyClassTwo("foo", "bar"); -------------------------------------------------------------------------------- /objects-introduction/class/sintactic_pov.js: -------------------------------------------------------------------------------- 1 | // class is a collection of methods grouped by a name 2 | // one of the method of a class is the constructor 3 | class myClass { 4 | // you cannot have more than one construtor 5 | constructor(value1, value2) { 6 | this.property1 = value1; 7 | this.property2 = value2; 8 | this.property3 = ""; 9 | } 10 | 11 | method1() {} 12 | method2() {} 13 | } 14 | 15 | myClass.prototype //? -------------------------------------------------------------------------------- /objects-introduction/class/sintactic_sugar.js: -------------------------------------------------------------------------------- 1 | class Person { 2 | constructor(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | } 7 | 8 | const ryanRay = new Person("Ryan", "Ray"); 9 | 10 | // we cannot invoke with a class like a function 11 | // const person = Person('personName', 'personSurname'); // error 12 | 13 | // this is equivalent to create the following: 14 | // function Person() { 15 | // "use strict"; 16 | // this.name = name; 17 | // this.surname = surname; 18 | // } 19 | 20 | // to check that is just syntactic sugar 21 | typeof Person; // function 22 | 23 | let PersonTwo = class { 24 | constructor(name, surname) { 25 | this.name = name; 26 | this.surname = surname; 27 | } 28 | }; 29 | 30 | let john = new PersonTwo("John", "Carter"); 31 | -------------------------------------------------------------------------------- /objects-introduction/class/static_method.js: -------------------------------------------------------------------------------- 1 | class User { 2 | constructor(name, lastname) { 3 | this.name = name; 4 | this.lastname = lastname; 5 | } 6 | } 7 | 8 | class Programmer extends User { 9 | constructor(language, name, lastname) { 10 | super(); 11 | this.language = language; 12 | this.name = name; 13 | this.lastname = lastname; 14 | } 15 | } 16 | 17 | const joe = new Programmer("Javascript", "Joe", "Doe"); 18 | joe; // 19 | -------------------------------------------------------------------------------- /objects-introduction/contructor/constructor.js: -------------------------------------------------------------------------------- 1 | // OBJECTS IN LITERAL NOTATION ARE NOT REUSABLE, WE HAVE TO REPEAT OBJECTS 2 | 3 | const johnSmith = { 4 | name: "John", 5 | surname: "Smith", 6 | address: { 7 | street: "13 Duncannon Street", 8 | city: "London", 9 | country: "United Kingdom", 10 | }, 11 | displayFullName: function () { 12 | return this.name + " " + this.surname; 13 | }, 14 | }; 15 | 16 | const marioRossi = { 17 | name: "Mario", 18 | surname: "Rossi", 19 | address: { 20 | street: "Piazza Colonna 370", 21 | city: "Roma", 22 | country: "Italy", 23 | }, 24 | displayFullName: function () { 25 | return this.name + " " + this.surname; 26 | }, 27 | }; 28 | 29 | // We can create objects with a constructor 30 | function Person() { 31 | this.name = ""; 32 | this.surname = ""; 33 | this.address = ""; 34 | this.email = ""; 35 | this.displayFullName = function () { 36 | return this.name + " " + this.surname; 37 | }; 38 | } 39 | 40 | // TO CREATE AN OBJECT WITH A CONSTRUCTOR. and we have TO USE new KEYWORD 41 | let ryanRay = new Person(); 42 | ryanRay.name = "Ryan"; 43 | ryanRay.surname = "Ray"; 44 | ryanRay.displayFullName(); 45 | 46 | let johnCarter = new Person(); 47 | johnCarter.name = "John"; 48 | johnCarter.surname = "Carter"; 49 | johnCarter.displayFullName(); 50 | -------------------------------------------------------------------------------- /objects-introduction/contructor/constructor_parameters.js: -------------------------------------------------------------------------------- 1 | // this constructor accetps parameters 2 | function Person(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | this.address = ""; 6 | this.email = ""; 7 | this.displayFullName = function () { 8 | return this.name + " " + this.surname; 9 | }; 10 | } 11 | 12 | const jamesWilson = new Person("James", "Wilson"); 13 | const gregoryHouse = new Person("Gregory", "House"); 14 | 15 | jamesWilson 16 | gregoryHouse -------------------------------------------------------------------------------- /objects-introduction/contructor/new.js: -------------------------------------------------------------------------------- 1 | // without new. is just an execution of a function 2 | // but this keyword takes context in the function, and assign global variables 3 | // in strict mode this have undefined value, and gives an error 4 | //try to access to a non existed property 5 | 6 | function Person() { 7 | "use strict"; 8 | this.name = ""; 9 | this.surname = ""; 10 | this.address = ""; 11 | this.email = ""; 12 | this.displayFullName = function () { 13 | return this.name + " " + this.surname; 14 | }; 15 | } 16 | 17 | // without new 18 | // const person = Person(); 19 | const person = new Person(); 20 | person; //? 21 | 22 | const mankind = { 23 | Person: function (name, lastname) { 24 | "use strict" 25 | this.name = name; 26 | this.lastname = lastname; 27 | }, 28 | }; 29 | 30 | const joe = mankind.Person("Joe", "Smith"); //? 31 | mankind -------------------------------------------------------------------------------- /objects-introduction/contructor/object_constructor.js: -------------------------------------------------------------------------------- 1 | // WE CAN USE THE OBJECT CONSTRUCTOR TO CREATE GENERIC OBJECTS 2 | let personA = new Object(); 3 | personA.name = "Ryan"; 4 | personA.surname = "Ray"; 5 | 6 | // it is the same than create with object literal 7 | // every object using literal notation have an object contructor 8 | const person = {}; 9 | console.log(person.constructor == Object); // true 10 | 11 | // object contructor can generate object from any javascript expression 12 | let number = new Object(12); 13 | let anotherNumber = new Object(3 * 2); 14 | let string = new Object("test"); 15 | let jhon = new Object({ name: "John", surname: "Smith" }); 16 | 17 | console.log(number); 18 | -------------------------------------------------------------------------------- /objects-introduction/contructor/prototype.js: -------------------------------------------------------------------------------- 1 | // this constructor accetps parameters 2 | function Person(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | this.address = ""; 6 | this.email = ""; 7 | } 8 | 9 | Person.prototype.displayFullName = function () { 10 | return this.name + " " + this.surname; 11 | }; 12 | 13 | const jamesWilson = new Person("James", "Wilson"); 14 | const gregoryHouse = new Person("Gregory", "House"); 15 | 16 | jamesWilson.displayFullName(); 17 | gregoryHouse.displayFullName(); 18 | 19 | Person.prototype; //? 20 | -------------------------------------------------------------------------------- /objects-introduction/literals.js: -------------------------------------------------------------------------------- 1 | // This is an object literal representation: { } 2 | console.log( { } ) 3 | 4 | // an object is a container of values combined to form a single data structured 5 | 6 | // Objects has properties and methods 7 | 8 | // Properties are data 9 | const person = { 10 | name: "John", // property 11 | surname: "Carter", 12 | }; 13 | 14 | // methods are just properties with function as value 15 | 16 | const person2 = { 17 | name: "Ryan", 18 | lastname: "Ray", 19 | sayHello: function () { 20 | return "Hello I'am Ryan" 21 | } 22 | } 23 | 24 | // an object is a *list* of key value pairs 25 | const user = { 26 | username: "fazt", // key: value 27 | email: "fazt@gmail.com", 28 | password: "somepassword", 29 | age: 100, 30 | hobbies: ["read", "programming", "run"] 31 | } 32 | 33 | console.log(user) 34 | 35 | // and like any list it could be empty 36 | const emptyObject = {} 37 | -------------------------------------------------------------------------------- /objects-introduction/methods.js: -------------------------------------------------------------------------------- 1 | // while properties represent data, methods represent actions 2 | function showFullName() { 3 | return "John Smith"; 4 | } 5 | 6 | const john = {}; 7 | john.showFullName = showFullName; 8 | 9 | // we can assign a method inside its literal representation 10 | 11 | const joe = { 12 | name: "Joe", 13 | surname: "Carter", 14 | showFullName: function () { 15 | return "Joe Carter"; 16 | }, 17 | }; 18 | 19 | // and in ECMASCRIPT 2015, we can assign a method this way as well 20 | const ryan = { 21 | name: "Ryan", 22 | surname: "Ray", 23 | showFullName() { 24 | return "Ryan Ray"; 25 | }, 26 | }; 27 | 28 | // the last method always display the same name, to avoid it, we can use this keyword 29 | ryan.name = "Braian" 30 | ryan.showFullName = function () { 31 | return this.name + " " + this.surname; 32 | }; 33 | 34 | -------------------------------------------------------------------------------- /objects-introduction/properties.js: -------------------------------------------------------------------------------- 1 | // the double quotes are optional in keys 2 | const john = { 3 | name: "John", 4 | surname: "Smith", 5 | }; 6 | 7 | // but if the name is not valid variable name, you have to use double quotes 8 | const joe = { 9 | "first-name": "joe", // double quotes are needed in "first-name" 10 | "second-name": "Carter", 11 | }; 12 | 13 | // nested object, you can assign an object to another object 14 | let ryan = { 15 | name: "Ryan", 16 | surname: "Ray", 17 | address: { 18 | // a nested object 19 | street: "13 duncannon Street", 20 | city: "London", 21 | country: "United Kingdom", 22 | }, 23 | }; 24 | 25 | // to access to values stored in properties you can use 26 | // dot notation 27 | let name = john.name; 28 | 29 | // or string in square brackets 30 | // this is required is the name is not a valid variable name 31 | let firstname = joe["first-name"]; 32 | 33 | // if we access to a non-existin property, we get undefined 34 | let age = joe.age; // undefined 35 | 36 | // if we assign a value to a not yet defined property, we actually create this property 37 | joe.age = 28; 38 | 39 | // we can start with a basic definition and gradually add more propertie 40 | let newUser = {}; 41 | 42 | newUser.name = "Greg"; 43 | newUser.surname = "House"; 44 | newUser.address = { 45 | street: "123 dunncannon street", 46 | city: "London", 47 | country: "United Kingdom", 48 | }; 49 | newUser.age = 28; 50 | 51 | // besides being able to create properties dinamically, we can destroy it 52 | delete newUser.age; 53 | -------------------------------------------------------------------------------- /objects-introduction/prototypes/extend_builtin.js: -------------------------------------------------------------------------------- 1 | // We can extend built-in object functionality using prototypes 2 | String.prototype.padLeft = function (width, char = " ") { 3 | let result = this; 4 | 5 | if (this.length < width) { 6 | result = new Array(width - this.length + 1).join(char) + this; 7 | } 8 | 9 | return result; 10 | }; 11 | 12 | "hello".padLeft(12, "x"); //? 13 | 14 | // but in javasrcipt you can use padstart and padEnd 15 | "hello".padEnd(12, "x"); //? 16 | "hello".padStart(12, "x"); //? 17 | -------------------------------------------------------------------------------- /objects-introduction/prototypes/prototype.js: -------------------------------------------------------------------------------- 1 | // we can change structure after the creation of an object 2 | function Person(name, surname) { 3 | this.name = name; 4 | this.surname = surname; 5 | } 6 | 7 | const john = new Person("John", "Smith"); 8 | const mario = new Person("Mario", "Rossi"); 9 | 10 | john.greets = function () { 11 | return `Hello ${this.name} ${this.surname}!`; 12 | }; 13 | 14 | // But, to change the structure of all Objects 15 | // created by a construtor 16 | 17 | Person.prototype.greets = function () { 18 | return `Hello I'am ${this.name} ${this.surname}!`; 19 | }; 20 | 21 | mario.greets() 22 | // the prototype is a reference to another Object 23 | // if a object dont have a method, it will find in the construtor 24 | // and if not, it will find in the basic Object methods -------------------------------------------------------------------------------- /oop-principles/Aggregation.js: -------------------------------------------------------------------------------- 1 | // aggregation is a special form of association relationship 2 | // where an object has a major role than other one 3 | 4 | // the major role determines the ownership of an object in relation to the other 5 | // the owner object is called aggregate and owned object is called component 6 | // but each object is independent 7 | 8 | // this is the owner 9 | const company = { 10 | name: "Fazt Tech", 11 | employees: [], 12 | }; 13 | 14 | class Person { 15 | constructor(name, surname) { 16 | this.name = name; 17 | this.surname = surname; 18 | } 19 | } 20 | 21 | // this are independent objects 22 | const johnSmith = new Person("John", "Smith"); 23 | const ryanRay = new Person("Ryan", "Ray"); 24 | 25 | company.employees.push(johnSmith); 26 | company.employees.push(ryanRay); 27 | 28 | // aggregation is a mechanism that allows us to create an object consist of several objects 29 | -------------------------------------------------------------------------------- /oop-principles/Encapsulation.js: -------------------------------------------------------------------------------- 1 | // encapsulation consist in hide complexity and group functionality and properties 2 | 3 | const company = { 4 | name: "Fazt Tech", 5 | employees: [], 6 | sortEmployees: function () {}, 7 | }; 8 | 9 | // when internal behavior depens on publick properties, we frustrate the the effor tho hide internal details 10 | 11 | company.employees = "some random string"; 12 | 13 | // company.sortEmployees() // error 14 | 15 | // we could prevent direct access to relevant properties, replacing them with methods 16 | 17 | function Company() { 18 | let employees = []; 19 | 20 | this.name = name; 21 | 22 | this.getEmployees = function () { 23 | return employees; 24 | }; 25 | 26 | this.addEmployee = function (employee) { 27 | employees.push(employee); 28 | }; 29 | 30 | this.sortEmployees = function () {}; 31 | } 32 | 33 | class Company2 { 34 | constructor(name) { 35 | this.name = name; 36 | this.employees= [] 37 | } 38 | 39 | getEmployees() { 40 | return this.employees; 41 | } 42 | 43 | addemployee(employee) { 44 | this.employees.push(employee); 45 | } 46 | 47 | sortEmployees() {} 48 | } 49 | 50 | // with this approach we need to use methods to obtain and add employees to the list 51 | const person = { 52 | name: "ryan", 53 | surname: "Ray", 54 | location: "London" 55 | } 56 | 57 | const company2 = new Company2("Fazt Web") 58 | company2.addemployee(person) 59 | company2.getEmployees() //? 60 | company2 61 | -------------------------------------------------------------------------------- /oop-principles/README.md: -------------------------------------------------------------------------------- 1 | # OOP Principles 2 | 3 | - asssociations 4 | - composition 5 | - aggregation 6 | - encapsulation 7 | - inheritance 8 | - polimorphism -------------------------------------------------------------------------------- /oop-principles/association.js: -------------------------------------------------------------------------------- 1 | 2 | // association is the relationship between two objects 3 | // each object is independent of each other 4 | // son an object can exists withouth the other 5 | 6 | class Person { 7 | constructor (name, surname) { 8 | this.name = name; 9 | this.surname = surname; 10 | this.parent = null; 11 | } 12 | } 13 | 14 | const johnSmith = new Person('John', 'Smith') //? 15 | const fredSmith = new Person('Fred', 'Smith') //? 16 | fredSmith.parent = johnSmith -------------------------------------------------------------------------------- /oop-principles/composition.js: -------------------------------------------------------------------------------- 1 | // composition is a strong type of aggregation. 2 | // each component has no independent life withot his owner 3 | 4 | const person = { 5 | name: "Ryan", 6 | surname: "Ray", 7 | address: { // this object depende on his owner, if the person is delted also this object 8 | street: "123 Baker Street", 9 | city: "London", 10 | country: "United Kingdom", 11 | }, 12 | }; 13 | -------------------------------------------------------------------------------- /oop-principles/inheritance.js: -------------------------------------------------------------------------------- 1 | // inheritance enables new objects to acquire properties of existen objects 2 | 3 | // this allows to us to avoid repeat code, for example if we have a programmer and person class 4 | 5 | // we could avoid to repeat the name and the surname 6 | 7 | // function Person () { 8 | // this.name = "" 9 | // this.surname = "" 10 | // } 11 | 12 | // function Programmer() { 13 | // this.language = "" 14 | // } 15 | 16 | // inherit properties from Person like name an surname 17 | // Programmer.prototype = new Person() 18 | 19 | // // create a new programmer 20 | // const programmer = new Programmer() 21 | 22 | // programmer.name = "Ryan" 23 | // programmer.surname = "Ray" 24 | // programmer.language = "Python" 25 | // programmer 26 | 27 | // and with classes 28 | 29 | class Person { 30 | constructor(name, surname) { 31 | this.name = name 32 | this.surname = surname; 33 | } 34 | } 35 | 36 | const person = new Person("Ryan", "Ray"); 37 | 38 | class Programmer extends Person { 39 | constructor(language) { 40 | super() 41 | this.language = language; 42 | } 43 | } 44 | 45 | const programmer = new Programmer() 46 | programmer.name = "Ryan" 47 | programmer.surname = "Ray" 48 | programmer.language = "Python" 49 | programmer 50 | 51 | // https://developer.mozilla.org/es/docs/Learn/JavaScript/Objects/Inheritance -------------------------------------------------------------------------------- /oop-principles/inheritance/inheritance_prototype.js: -------------------------------------------------------------------------------- 1 | function User(name, lastname, age) { 2 | this.name = name; 3 | this.lastname = lastname; 4 | this.age = age; 5 | } 6 | 7 | User.prototype.displayName = function () { 8 | return this.name + " " + this.lastname; 9 | }; 10 | 11 | function Programmer(language, name, lastname, age) { 12 | // User.call(this, "Ryan", "Ray", "Python"); 13 | User.call(this, name, lastname, age); 14 | this.language = language; 15 | } 16 | 17 | 18 | Programmer.prototype = Object.create(User.prototype); 19 | 20 | Programmer.prototype.constructor // User 21 | Programmer.prototype.constructor = Programmer; 22 | Programmer.prototype.constructor 23 | 24 | const joe = new User("Joe", "Doe", 30); 25 | 26 | const ryanProgrammer = new Programmer("Python"); //? 27 | ryanProgrammer.name = "Ryan"; 28 | ryanProgrammer.lastname = "Ray"; 29 | ryanProgrammer.age = "30"; 30 | 31 | ryanProgrammer.displayName(); //? 32 | -------------------------------------------------------------------------------- /oop-principles/inheritance/object_create.js: -------------------------------------------------------------------------------- 1 | const userProtos = { 2 | getAge: function () { 3 | return this.age; 4 | }, 5 | displayName: function () { 6 | return this.firstName + " " + this.lastName; 7 | }, 8 | }; 9 | 10 | const john = Object.create(userProtos, { 11 | firstName: { value: "John" }, 12 | lastName: { value: "Doe" }, 13 | age: { value: 30 }, 14 | }); 15 | 16 | john.displayName(); //? 17 | john.getAge(); //? 18 | -------------------------------------------------------------------------------- /oop-principles/polymorphism.js: -------------------------------------------------------------------------------- 1 | // is the hability to handle multuple data types uniformly 2 | 3 | // it allow us to create more compact code 4 | 5 | // common ways to support polymorphism in languages 6 | // methods that take parameters with different types (overloading) 7 | // management of generic type, not known in advance (parametric polymorphism) 8 | // expressions whose type can be represented by a class and clases derived from it (subtype polymorphism) and inclusion polymorphism) 9 | 10 | // x could be an string or a numeric value 11 | function countItems(x) { 12 | return x.toString().length; 13 | } 14 | 15 | countItems(3); 16 | countItems("hello world"); 17 | 18 | function sum(x, y, z) { 19 | x = x ? x : 0; 20 | y = y ? y : 0; 21 | z = z ? z : 0; 22 | return x + y + z; 23 | } 24 | 25 | sum(1, 3, 6); 26 | sum(10, 20); 27 | sum(3); 28 | 29 | function sum2(x = 0, y = 0, z = 0) { 30 | return x + y + z; 31 | } 32 | 33 | const sum3 = (x = 0, y = 0, z = 0) => x + y + z; 34 | 35 | // javasrcipt support polymorphism in a more direct way than strong type languages 36 | 37 | /// Parametric Polimorphism 38 | // Parametric polymorphism allow us to work with parameters of any type 39 | 40 | function Person(name, surname) { 41 | this.name = name 42 | this.surname = surname 43 | } 44 | 45 | function Programmer(language) { 46 | this.language = language 47 | } 48 | 49 | Programmer.prototype = new Person() 50 | 51 | function writeFullName(p) { 52 | return p.name + " " + p.surname 53 | } 54 | 55 | const john = new Person("John", "Cartar") 56 | 57 | const ryan = new Programmer("Python") 58 | ryan.name ="Ryan" 59 | ryan.surname="Ray" 60 | 61 | 62 | writeFullName(john) //? 63 | writeFullName(ryan) //? -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "javascript-oop-1", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "javascript-oop-1", 3 | "version": "1.0.0", 4 | "description": "- Basic Knowledge of Javascript Syntax", 5 | "main": "index.js", 6 | "type": "module", 7 | "scripts": { 8 | "start": "node index.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/FaztWeb/javascript-oop.git" 13 | }, 14 | "keywords": [], 15 | "author": "", 16 | "license": "ISC", 17 | "bugs": { 18 | "url": "https://github.com/FaztWeb/javascript-oop/issues" 19 | }, 20 | "homepage": "https://github.com/FaztWeb/javascript-oop#readme" 21 | } 22 | --------------------------------------------------------------------------------