Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我不能用javascript传递这个函数?_Javascript - Fatal编程技术网

为什么我不能用javascript传递这个函数?

为什么我不能用javascript传递这个函数?,javascript,Javascript,我无法将document.getElementById作为参数传递: 鉴于此HTML: <div id="el"></div> 小提琴: 例如,我在Chrome中得到一个“uncaughtTypeError:非法调用” 为什么会这样?document.getElementById需要以document作为上下文执行,您可以使用bind修复此问题: test(document.getElementById.bind(document)); document.getEl

我无法将
document.getElementById
作为参数传递:

鉴于此HTML:

<div id="el"></div>
小提琴:

例如,我在Chrome中得到一个“uncaughtTypeError:非法调用”


为什么会这样?

document.getElementById需要以document作为上下文执行,您可以使用bind修复此问题:

test(document.getElementById.bind(document));

document.getElementById需要在文档作为上下文的情况下执行,您可以使用bind修复该问题:

test(document.getElementById.bind(document));
以这个例子为例

var item=“这是一个全局变量”;
变量对象={
项目:“价值”,
findInThing:函数(prop){
归还这个[道具];
}
}
警报(thing.findInThing(“项目”);
函数调用(a_func){
返回一个函数(“项目”);
}
警报(呼叫(thing.findInThing))以这个例子为例

var item=“这是一个全局变量”;
变量对象={
项目:“价值”,
findInThing:函数(prop){
归还这个[道具];
}
}
警报(thing.findInThing(“项目”);
函数调用(a_func){
返回一个函数(“项目”);
}

警报(呼叫(thing.findInThing))因为javascript:-),因为您传递的函数没有任何上下文。您可以用
document.getElementById.bind(document)
绑定它,因为“未捕获类型错误:非法调用”;)因为javascript:-),因为您传递的函数没有任何上下文。您可以用
document.getElementById.bind(document)
绑定它,因为“未捕获类型错误:非法调用”;)