16
loading...
This website collects cookies to deliver better user experience
Object()
constructor function
const myObject = {}; // Using literal notation
const myObject = new Object(); // Using the Object() constructor function
Object()
constructor function is a bit slower and verbose. const souvik = {};
souvik.learning= true;
souvik["status"] = "Learning and implementing";
souvik.work = function () {
console.log("Working as Full Stack Web Dev!");
};
{
learning: true,
status: "Learning and implementing",
work: function () {
console.log("Working as Full Stack Web Dev!");
}
}
const souvik = {
learning: true,
status: "Learning and implementing",
work: function () {
console.log("Working as Full Stack Web Dev!");
}
}
souvik.learning = false;
souvik["status"] = "Building projects";
delete
operator.delete souvik.learning; //true
let originalObject = {
status: "online"
};
function setToOffline(object) {
object.status = "offline";
}
setToOffline(originalObject);
originalObject.status; // "offline"
function changeToEight(n) {
n = 8; // whatever n was, it is now 8... but only in this function!
}
let n = 7;
changeToEight(n);
console.log(n); // 7
this
.const souvik = {
learning: true,
status: "Learning",
work: function () {
console.log(`${this.status} Full Stack Web Dev!`);
}
}
souvik.work() //Learning Full Stack Web Dev!
this
keyword helps an object to access and manipulate its own properties. This way of invoking a method using dot operator is known as Implicit binding where this
refers to the object using which the method is invoked.this
will point to some other objects using call(), apply() and bind() methods - which is known as Explicit binding. To know more read call, bind and apply in JS article.this
keyword, I have a question for you, what would be the output if we invoke exploringThis
function?function exploringThis() {
console.log(this)
}
exploringThis();
this
points to the global object window
. window
object is provided by the browser and globally accessible by JS code using the window
keyword. This object is not part of the JavaScript specification.
window
object.var learning = "objects in JS";
function work() {
console.log("Writing blog on JS objects")
}
window.learning === learning; //true
window.work === work; //true
Only declaring variables with the var
keyword will add them to the window
object. If you declare a variable with let
or const
, it won't be added as the property to the window
object.
let learning = "objects in JS";
window.learning === learning; //false
object()
constructor function that can be used to create a new empty object, has some methods of its own. These methods are:const souvik = {
learning: true,
status: "Learning",
}
Object.keys(souvik); // ["learning", "status"]
Object.values(souvik); // [true, "Learning"]