DEV Community

ana
ana

Posted on

JavaScript function call() method:

Image description

imaging that: you have some object in your program(with JS language) and you have some name:value in your object with any data type.
if we say any data type that can be to this mean we want write a name:value and "value" be a function?
Ofcourse!!

if function situation is to be that, function take two parameters and then return their in output, and if we want specify our parameters from a another object we can use call method!

example:

const x = {
fullName: function(){
return this.name + " " + this.lName
}
};
Enter fullscreen mode Exit fullscreen mode

But where is name or lName(last name)?

is here: (in another object, we take from this object)

const object1 = {
name: "ana",
lName: "amini"
}
Enter fullscreen mode Exit fullscreen mode

So now we achieve to method part:

console.log(x.fullName.call(object1));
Enter fullscreen mode Exit fullscreen mode

and then output will be like that:

ana amini
Enter fullscreen mode Exit fullscreen mode

Tip(1): for showing a value of the name in object we use from this rule: objectName.name1;

Tip(2): we use " " in our function, so our values are separate from each other!

;)!

Top comments (2)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
anateotfw profile image
ana

thank you!