I'm studying Javascript, and I found this example on my book
function sayNameForAll(label) {
console.log(label + ":" + this.name);
}
var person1 = {
name: "Nicholas"
};
var person2 = {
name: "Greg"
};
var name = "Michael";
sayNameForAll.call(this, "global");
sayNameForAll.call(person1, "person1");
sayNameForAll.call(person2, "person2");
This outputs:
"global:Michael"
"person1:Nicholas"
"person2:Greg"
I understand how call
works, but the output of sayNameForAll.call(this, "global");
isn't global:Michael
but global:result
.
this is code http://jsfiddle.net/rho3zyb4/