Object is a non primitive data type in javascript.
With JavaScript, one can define and create own objects.
There are different ways to create new objects:
- Define and create a single object, using an object literal.
- Define and create a single object, with the keyword new.
- Define an object constructor, and then create objects of the constructed type.
Using an object literal, you both define and create an object in one statement.
let user = { // an object
name: "John", // by key "name" store value "John"
age: 30 // by key "age" store value 30
};
A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. The properties of an object define the characteristics of the object. Properties can be access with a '.' operator
let user = {
name: "John",
age: 30
};
console.log(user.name) // John
Unassigned properties of an object are undefined
user.city; // undefined
hey are addressed by reference, not by value.
let user = {
name: "John",
age: 30
};
let user2=user // This will not create a copy of user.
console.log(user) // {name: "John", age: 30}
console.log(user2) // {name: "John", age: 30}
user2.city='Delhi'
console.log(user2) // {name: "John", age: 30, city: "Delhi"}
console.log(user) // {name: "John", age: 30, city: "Delhi"}
// Both user and user2 are the same object.
It follows two steps:
- Define the object type by writing a constructor function.
- Create an instance of the object with new.
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var mycar = new Car('Eagle', 'Talon TSi', 1993);
console.log(mycar) // Car {make: "Eagle", model: "Talon TSi", year: 1993}make: "Eagle"model: "Talon TSi"year: 1993__proto__: constructor: ƒ Car(make, model, year)arguments: nullcaller: nulllength: 3name: "Car"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: VM42:1[[Scopes]]: Scopes[1]__proto__: Object
mycar.color = 'black';
console.log(mycar) // Car {make: "Eagle", model: "Talon TSi", year: 1993, color: "black"}
The following examples store an empty Object object in o:
let obj = new Object() // {}
let obj = new Object(undefined) // {}
let obj = new Object(null) // {}
Using Object to create Boolean objects The following examples store Boolean objects in obj:
let obj = new Object(true) // Boolean {true}
let obj = new Object(Boolean()) // Boolean {false}
This method is useful to create objects, because it allows us to choose the prototype object for the object we want to create, without having to define a constructor function.
// Animal properties and method encapsulation
var Animal = {
type: 'Invertebrates', // Default value of properties
displayType: function() { // Method which will display type of Animal
console.log(this.type);
}
};
// Create new animal type called animal1
var animal1 = Object.create(Animal);
animal1.displayType(); // Output:Invertebrates
// Create new animal type called Fishes
var fish = Object.create(Animal);
fish.type = 'Fishes';
fish.displayType(); // Output:Fishes
A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property.
var obj = {
a: 7,
get getA() {
return this.a + 1;
},
set setA(x) {
this.a = x / 2;
}
};
console.log(obj.a); // 7
console.log(obj.getA); // 8 <-- At this point the get getA() method is initiated.
o.setA = 50; // <-- At this point the set setA(x) method is initiated
console.log(o.getA); // 25
- Object.assign() Copies the values of all enumerable own properties from one or more source objects to a target object.
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target); // { a: 1, b: 4, c: 5 }
console.log(returnedTarget); // { a: 1, b: 4, c: 5 }
-
Object.defineProperty() Adds the named property described by a given descriptor to an object.
-
Object.freeze() Freezes an object. Other code cannot delete or change its properties.
-
Object.getOwnPropertyDescriptor() Returns a property descriptor for a named property on an object.
-
Object.getOwnPropertyDescriptors() Returns an object containing all own property descriptors for an object.
-
Object.getPrototypeOf() Returns the prototype (internal [[Prototype]] property) of the specified object.
-
Object.isExtensible() Determines if extending of an object is allowed.
-
Object.isFrozen() Determines if an object was frozen.
-
Object.isSealed() Determines if an object is sealed.
-
Object.keys() Returns an array containing the names of all of the given object's own enumerable string properties.
-
Object.seal() Prevents other code from deleting properties of an object.
-
Object.setPrototypeOf() Sets the object's prototype (its internal [[Prototype]] property).
-
Object.values() Returns an array containing the values that correspond to all of a given object's own enumerable string properties.