Tuesday, March 3, 2020

JavaScript - Object


Creating a JavaScript Object
var Employee = {firstName:"John",
                lastName:"Doe",
                age:50,
                eyeColor:"Black",
                salary:"60000"};

var Employee = {
  firstName: 
"John",
  lastName: 
"Doe",
  age: 
50,
  eyeColor: 
"blue",
  salary:"60000"
};

var Employee = new Object();
Employee.firstName = 
"John";
Employee.lastName = 
"Doe";
Employee.age = 
50;
Employee.eyeColor = 
"Black";
Employee.salary = "60000";

JavaScript Objects are Mutable

var person = {firstName:"John",
              lastName:"Doe",
              age:50,
              eyeColor:"blue",
              salary:"60000"}

var x = person;
x.age = 
10;           // This will change both x.age and person.age

Javascript - Prototype


<!DOCTYPE html>
<html>
<head>

</head>
<body>

    <script>

        // function constructor
        function Employee(name, job, yearOfBirth) {
            this.name = name;
            this.job = job;
            this.yearOfBirth = yearOfBirth;
        }
        // adding calculateAge() method to the Prototype property
        Employee.prototype.calculateAge = function () {
            console.log('The current age is: ' + (2019 - this.yearOfBirth));
        }
        console.log(Employee.prototype);

        // creating Object Person1
        let Employee1 = new Employee('Elon', 'Astronaut', 1986);
        console.log(Person1)
        let Employee2 = new Employee('ET', 'Alien', 1997);
        console.log(Person2)

        Employee1.calculateAge();
        Employee2.calculateAge();

    </script>

</body>
</html>

JavaScript - Object

Creating a JavaScript Object var  Employee = {firstName: "John" ,                 lastName: "Doe" ,             ...