Object in JS
Object is the most important data-type for modern JavaScript that contain any combination of data-types in the form of “key: value” pairs. These keys can be variables or functions in the term of an object that can be created with curly brackets {…}
<script>
const person={
name:’shairy’,
designation:[‘professor’,’Developer’],
working:function(){
console.log(“i am working in college”);
},
profession() {
console.log(“i am working as an assistant professor”);
}
};
person.working();
person.profession();
console.log(person.name);
console.log(person[‘designation’]);
</script>
Object in JS using this and call with the help of bind
<script>
const person={
name:’shairy’,
designation:’professor’,
working:function(){
console.log(this);
}
}
person.working();
//Here output is of person object due to use of this
let work= person.working;
//This line not calling function just ref to the function now work variable is function
work();// its gives window object
console.log(work);
work = person.working.bind(person);
//Function in javascript is an object but with the bind method we can set the value of this permanatly, after setting bind work is also object person not window
work();
</script>