Inherited Properties
Inherited properties of an object are those properties that have been inherited from the object’s prototype, as opposed to being defined for the object itself, which is known as the object’s Own property. To verify if a property is an object’s Own property, we can use the hasOwnProperty method. Property Attributes Data properties in JavaScript have four attributes.
- value: The property’s value.
- writable: When true, the property’s value can be changed
- enumerable: When true, the property can be iterated over by “for-in” enumeration. Otherwise, the property is said to be non-enumerable.
- configurable: If false, attempts to delete the property, change the property to be an access-or property, or change its attributes (other than [[Value]], or changing [[Writable]] to false) will fail.
Example: Below is the example of inherited properties.
// hasOwnProperty code in js
const object1 = new Object();
object1.property1 = 42;
console.log(object1.hasOwnProperty('property1')); // true
Output
true
Creating Objects: For creating objects refer to the following article. Ref:- Creating objects in JavaScript
Objects in Javascript
Objects, in JavaScript, are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScript’s primitive data types (Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data types all store a single value each (depending on their types).
While these primitive data types can each hold only a single value, objects are like containers that can hold multiple values at once. These values are stored as properties of the object, each with its own key. This allows us to group related data and functions together, making our code more organised and easier to understand.
Syntax:
new Object(value)
Object(value)
let object_name = {
key_name : value,
...
}
Note:- Object() can be called with or without new. Both create a new object.
Example: Below is an example of Objects in JavaScript.
const o = new Object();
o.foo = 42;
console.log(o);
// { foo: 42 }
Output
{ foo: 42 }
Example: In this example “name”, “location”, and “established” are all “keys” and “Vivekananda School”, “Delhi” and 1971 are values of these keys respectively. Each of these keys is referred to as properties of the object. An object in JavaScript may also have a function as a member, in which case it will be known as a method of that object. Here “displayinfo” is a method of the school object that is being used to work with the object’s data, stored in its properties.
// JavaScript code demonstrating a simple object
let school = {
name: 'Vivekananda School',
location: 'Delhi',
established: '1971',
displayInfo: function () {
console.log(`${school.name} was established
in ${school.established} at ${school.location}`);
}
}
school.displayInfo();
Output
Vivekananda School was established in 1971 at Delhi
- Objects are more complex and each object may contain any combination of these primitive data-types as well as reference data-types.
- An object is a reference data type. Variables that are assigned a reference value are given a reference or a pointer to that value. That reference or pointer points to the location in memory where the object is stored. The variables don’t actually store the value.
- Loosely speaking, objects in JavaScript may be defined as an unordered collection of related data, of primitive or reference types, in the form of “key: value” pairs. These keys can be variables or functions and are called properties and methods, respectively, in the context of an object.
An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where a key is a string or a symbol (also called a “property name”), and the value can be anything including numbers, strings, booleans, functions, arrays, or even other objects. .