无法在 javascript 中复制内置函数
Can't replicate inbuilt function in javascript
Why do built-in functions not have a prototype property?
我在上面看到 post link 但它没有解决我的问题。
我能够复制一些内置对象,例如
var w=window;
w.alert("hi");
var d=document;
console.log(d.getElementById);
但是我不能复制函数
var a=document.getElementById;
console.log(a);
原型被复制但调用时它不起作用
var a=document.getElementById;
console.log(a('id'));
我用它来降低编码工作量。我知道我可以通过使用
来实现
function a(id){
return document.getElementById(id);
}
console.log(a('id'));
但这不是我要找的。有没有其他方法可以复制函数
@Xufox ,这是有效的。非常感谢
var a = document.getElementById.bind(document);
console.log(a('id'));
Why do built-in functions not have a prototype property?
我在上面看到 post link 但它没有解决我的问题。
我能够复制一些内置对象,例如
var w=window;
w.alert("hi");
var d=document;
console.log(d.getElementById);
但是我不能复制函数
var a=document.getElementById;
console.log(a);
原型被复制但调用时它不起作用
var a=document.getElementById;
console.log(a('id'));
我用它来降低编码工作量。我知道我可以通过使用
来实现function a(id){
return document.getElementById(id);
}
console.log(a('id'));
但这不是我要找的。有没有其他方法可以复制函数
@Xufox ,这是有效的。非常感谢
var a = document.getElementById.bind(document);
console.log(a('id'));