Objects
In the context of a web page, a JavaScript object is any scriptable HTML element. Outside the context of a web page, these 'core objects' are not associated with HTML elements, but with the language itself e.g. date and function.Object Properties
A JavaScript property has a similar relationship to the object it belongs to in the way that an HTML tag attribute has to the tag that contains it. For example, JavaScript "src" property is to an image object as the HTML "src" attribute is to an image tag.
Property: document.getElementById("myImg1").src = "art/97.webp"
Attribute: <img> id="myImg1" src="art/97.webp"></img>.
Object Methods
Within a web page, methods cause an otherwise static HTML document to react to the end user. This results in a meaningful experience for the end user which would otherwise be completely one-sided e.g. alert(), write() and focus().
Say a traffic light is a JavaScript object. Two properties of the traffic light object would be position and state. The position property doesn't really do anything, it is just a characteristic of the traffic light - so it is not an object in and of itself, it is only a property of the traffic light. The state however has its own special characteristics, so it is also considered an object. A state of the traffic light object would be considered a state method. The syntax for telling the traffic light to alert the color 'Red' in JavaScript might look like: trafficLight.state.alert("Red").
Encapsulation describes the idea of bundling data and methods that work on that data within one unit, say, a class. Reasons for using encapsulation:
• Better control of class properties and methodsAbstraction means to perceive an entity in a system or context from a particular perspective. We take out unnecessary details and only focus on aspects that are necessary to that context or system under consideration. JavaScript doesn't have the abstract class and interface features normally directly used in abstraction. However, abstract classes with interfaces are implemented by JavaScript Object.create method and prototypes. Abstraction and encapsulation are complementary concepts. Reasons for using abstraction:
• To achieve security by hiding certain detailsInheritance lets programmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.
Polymorphism lets programmers use the same word to mean different things in different contexts.