Greetings! I am currently delving into the realm of JavaScript, hailing from a C++ background. The transition has proven to be quite perplexing for me. Below is a snippet of code that I have been troubleshooting:
var someArray = [];
nameCompare = function(this.first,this.second)
{
if(first.name == second.name)
{
return 0;
}
else if(first.name < second.name)
{
return -1;
}
else
{
return 1;
}
};
someArray.tempSort = function(){
return this.sort(nameCompare);
};
var someOtherArray= Object.create(someArray);
someOtherArray.push({name: 'A', price: 3, rating: 2, distance: 2},
{name: "B", price: 1, rating: 5, distance: 1},
{name: 'C', price: 2, rating: 3, distance: 5},
{name: 'D', price: 5, rating: 4, distance: 8});
console.log(someOtherArray.tempSort());
I find myself bewildered by the implementation of the 'this' keyword in this context. As someArray inherits properties from someOtherArray and I aim to sort based on name, I struggle with correctly referencing the name property from someOtherArray. While I grasped that the 'this' keyword points to the current object, my understanding still eludes me. Additionally, I continuously encounter an error pertaining to missing formal parameters. Is it not feasible to pass 'this.first' and 'this.second' through the auxiliary function?