JavaScript is a multi-paradigm, dynamic language with types and operators, standard built-in objects, and methods. Its syntax is based on the Java and C languages — many structures from those languages apply to JavaScript as well. JavaScript supports object-oriented programming with object prototypes, instead of classes. JavaScript also supports functional programming — functions are objects, giving functions the capacity to hold executable code and be passed around like any other object.
Objects in Javascript
Objects allow us to group values—including other objects—to build more complex structures. he values are written as name : value pairs (name and value separated by a colon). Basically a JavaScript object is a collection of named values.
Creating and Mutating Objects
// Using an Object Literal var person = {name:"Sam", age:22, eyeColor:"brown"}; var person = { name:"Sam", age:22, eyeColor:"brown" }; // Using the JavaScript Keyword new var person = new Object(); person.name = "Sam"; person.age = 22; person.eyeColor = "brown"; person.name + " is " + person.age; //> "Sam is 22" person["firstname"] + " is " + person["age"]; //> "Sam is 22" person.eyeColor = "unnecessary field"; console.log(person); //> {name:"Sam", age:22, eyeColor:"unnecessary field"} delete person.eyeColor; console.log(person); //> {name:"Sam", age:22}
Methods
A JavaScript method is a property containing a function definition. JavaScript methods are actions that can be performed on objects.
In a function definition, ‘this’ keyword refers to the owner of the function. When an object owns a function in itself, ‘this’ keyword refers to that object.
var person = { name:"Sam", age:22, greet: function() { console.log("Hello " + this.name); } }; person.greet(); //> "Hello Sam" person.bye = function () { console.log("Bye " + this.name); }; person.bye(); //> "Bye Sam"