Search code examples
javascriptfunctiondomreplicationbuilt-in

Can't replicate inbuilt function in javascript


Why do built-in functions not have a prototype property?
I see a post on the above link but it is not solving my problem.

I am able to replicate some built in objects like

var w=window;
w.alert("hi");
var d=document;
console.log(d.getElementById);

But I can't replicate functions

var a=document.getElementById;
console.log(a);

The prototype gets copied but when invocating It is not working

var a=document.getElementById;
console.log(a('id'));

I use this to lower the coding effort. I know that I can achieve this by using

function a(id){
 return document.getElementById(id);
}
console.log(a('id'));

But this is not what I am looking for. Is there any other way to replicate a funciton


Solution

  • @Xufox , This is working. Thanks a lot

    var a = document.getElementById.bind(document);
    console.log(a('id'));