我如何将testx函数作为参数传递给change_text函数?
function change_text(to, id, func) {
this.to = to;
this.id = id;
this.doit = function() {
this.target = document.getElementById(this.id);
this.target.innerHTML = this.to;
}
func;
}
function testx() {
alert("TESTING");
}
var box = new change_text("HELLO, WORLD", 'theboxtochange', 'testx()');
Run Code Online (Sandbox Code Playgroud)
通过给它的名字(没有parens或引号):
var box = new change_text("HELLO, WORLD", 'theboxtochange', testx);
Run Code Online (Sandbox Code Playgroud)
函数是第一类对象,因此它们的名称是对它们的引用.
在内部change_text,您可以通过使用对它的引用来调用它(func),就像指向函数的任何其他符号一样,所以:
func();
Run Code Online (Sandbox Code Playgroud)