I was try to do the native implementation of call method in javascript. Here we need to handle the object which we pass in the call function and additional params. i handle the additional params using eval() method. is there any efficient way to do the native call() function implementation. here is my code
var name = {
name: "JavaScript",
version: "6",
}
function printName(location, district){
alert(this.name + ", " + this.version + ", " + location + ", " + district);
}
Function.prototype.myCall = function(...args){
var param = args.slice(1),
paramLength = param.length,
paramString = "JSONarg.myFun(";
for(var i = 1; i <= paramLength; i++){
paramString += "args["+i+"],";
}
paramString += ")";
if(typeof this != 'function'){
throw new Error(this + " is not a Function");
}
var JSONarg = {
...args[0],
myFun: this
}
return eval(paramString);
}
printName.myCall(name, "Chrome", "browser");
eval(). It's almost certainly not JavaScript code.