Skip to content

Commit a5fecb1

Browse files
authored
Update js codes.docx
1 parent 9d4c7db commit a5fecb1

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

‎js codes.docx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,3 +2620,60 @@ var store = sum(2);
26202620
console.log(store)
26212621
console.log(store(5))
26222622
=========================================================================
2623+
Call, apply and bind:
2624+
var userDetails ={
2625+
name: "Priya",
2626+
age: 100,
2627+
role: "Software Developer",
2628+
printDetail : function(){
2629+
console.log(this)// this representing userDetails whole object
2630+
}
2631+
}
2632+
userDetails.printDetail();
2633+
2634+
var userDetails2 ={
2635+
name: "Riya",
2636+
age: 101,
2637+
role: "Software Developer"
2638+
}
2639+
2640+
userDetails.printDetail.call(userDetails2); //if i want line 6th "this" should point to userDetails2 then use call function.
2641+
-----------------------------------------------------------------------------------
2642+
var userDetails ={
2643+
name: "Priya",
2644+
age: 100,
2645+
role: "Software Developer"
2646+
}
2647+
let printDetail = function(){
2648+
console.log(this.name)
2649+
}
2650+
printDetail.call(userDetails); // if function is independented of object.
2651+
2652+
var userDetails2 ={
2653+
name: "Riya",
2654+
age: 101,
2655+
role: "Software Developer"
2656+
}
2657+
printDetail.call(userDetails2);
2658+
--------------------------------------------------------------------------------
2659+
var userDetails ={
2660+
name: "Priya",
2661+
age: 100,
2662+
role: "Software Developer"
2663+
}
2664+
let printDetail = function(country){
2665+
console.log(this.name+" "+country)
2666+
}
2667+
printDetail.call(userDetails, "India"); // if function is independented of object.
2668+
2669+
var userDetails2 ={
2670+
name: "Riya",
2671+
age: 101,
2672+
role: "Software Developer"
2673+
}
2674+
2675+
printDetail.call(userDetails2, "India");
2676+
printDetail.apply(userDetails2, ["India"]);
2677+
let bindfunc= printDetail.bind(userDetails2, "India"); //creating a copy and invoke whenever require //don't call the diectly here like call function
2678+
bindfunc();
2679+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)