Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.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 存储在变量中的JS函数不可调用_Javascript_Function - Fatal编程技术网

Javascript 存储在变量中的JS函数不可调用

Javascript 存储在变量中的JS函数不可调用,javascript,function,Javascript,Function,我已经为我的网站建立了一个打字机效果,我想为某些打字机效果提供传递某些条件的能力,这样它们只有在条件成立时才会被执行 我试图通过将函数传递给构造函数来实现这一点,我想稍后调用该函数。不幸的是,无论出于何种原因,这都不起作用。。您可以在下面找到一个可复制的示例 结构如下所示: const Typewriter = function(..., condition, ....) { .... this.condition = condition; .... this.type(

我已经为我的网站建立了一个打字机效果,我想为某些打字机效果提供传递某些条件的能力,这样它们只有在条件成立时才会被执行

我试图通过将函数传递给构造函数来实现这一点,我想稍后调用该函数。不幸的是,无论出于何种原因,这都不起作用。。您可以在下面找到一个可复制的示例

结构如下所示:

const Typewriter = function(..., condition, ....) {
   ....
   this.condition = condition;
   ....
   this.type()
}
我这样传递函数:

const conditionFunc = function() { ... }

new Typewriter(...., conditionFunc, ...);
当我想稍后在
Typewriter.prototype()
中调用该函数时,我不能调用它,因为我可以使用
this
访问Typewriter对象

type
只是一个实现键入效果的函数

Typewriter.prototype.type = function() {
    // Add char to the element
    this.currValue = this.wordsToPrint.substring(0, this.currValue.length + 1);
    this.element.innerHTML = this.currValue;

    // console.log(this.condition()) gives error....
    setTimeout(() => this.type(), 80);
}
我尝试调用
type()
中的函数,就像这样:
this.condition()
但它说这不是一个函数。。。 当我尝试
this.condition
时,它显然会返回函数名和函数体,而不调用任何东西

在我的愤怒中,我尝试了类似于
var test=this.condition
test()
的东西,但这会导致同样的错误

我真的不知道它为什么会出现这样的错误,因为
typeof(this.condition)
返回
函数
显然

谢谢你的帮助


我将通过提供脚本代码,尝试给出一些最小的可复制示例:

const Typewriter=函数(元素、字停止打印、条件){
this.element=元素;
this.wordsToPrint=wordsToPrint;
这个条件=条件;
this.currValue='';
this.type();
}
//打字效果的方法
Typewriter.prototype.type=函数(){
//当条件保持时,向元素添加char
如果(this.condition()){//这是代码中的关键点
this.currValue=this.wordsToPrint.substring(0,this.currValue.length+1);
this.element.innerHTML=this.currValue;
}
setTimeout(()=>this.type(),80);
}
//加载DOM后设置打字机
document.addEventListener(“DOMContentLoaded”,init);
函数init(){
常量someElement={
元素:document.querySelector(“h1”),
内容:document.querySelector(“h1”).innerHTML
}
//在生效前清除元素的文本
someElement.element.innerHTML='';
const conditionFunc=函数(){
返回true;
}
//创作打字机
新打字机(someElement.element、someElement.content、conditionFunc);
}

这应该用打字机打印

一旦纠正了一些错误(列表中“h1和坏分隔符”之后的错误语法),它对我有效…试着按下运行按钮:

const Typewriter=函数(元素、字停止打印、条件){
this.element=元素;
this.wordsToPrint=wordsToPrint;
这个条件=条件;
this.currValue='';
this.type();
}
//打字效果的方法
Typewriter.prototype.type=函数(){
//当条件保持时,向元素添加char
如果(this.condition()){//这是代码中的关键点
this.currValue=this.wordsToPrint.substring(0,this.currValue.length+1);
this.element.innerHTML=this.currValue;
}
setTimeout(()=>this.type(),80);
}
//加载DOM后设置打字机
document.addEventListener(“DOMContentLoaded”,init);
函数init(){
常量someElement={
元素:document.querySelector(“h1”),
内容:document.querySelector(“h1”).innerHTML,
};
//在生效前清除元素的文本
someElement.element.innerHTML='';
const conditionFunc=function(){return Math.random()>0.5?true:false;}
//创作打字机
新打字机(someElement.element、someElement.content、conditionFunc);
}

这应该用打字机打印

此外,您只需将羽状箭头函数分配给常量即可

    const myFunc = (arg1, agr2) => { //blah blah }

我检查了我的原始脚本与我在这里提供的脚本之间的差异,基本上我有两个打字机对象,而其中一个没有任何条件,因此我将null传递给它的构造函数(很可能这不是最好的方法,如果JavaScript中有可选参数,需要谷歌搜索…)

Typewriter.prototype.type()
函数显然对两台打字机都运行,这就是它试图调用
this.condition()的原因
在Typewriter对象上,其中条件为
null
,这导致了错误。它在另一个Typewriter对象上工作得非常好,具有条件函数,但是第一个Typewriter对象在第二个Typewriter对象可以向控制台提供任何输出之前已经导致了错误,所以我认为它不工作一点也不奇怪


非常初学者的错误,非常抱歉。感谢您的帮助!

这确实应该有效。请发布一个允许我们重现问题的帖子。这会产生错误。从代码中获取
语法错误:无效或意外的标记
。请提供一个可执行示例,前提是您修复了小语法错误您的错误这似乎是可行的:只是尝试了您的脚本,它工作得很好。我只是将函数体更改为
{return true}
。但是在您的示例中,
someElement
对象中有三件事需要更正:您没有关闭第一个querySelector中的
双引号(
元素:document.querySelector(“h1)
),它后面也缺少一个逗号,在
内容
属性后面有一个
,您应该删除它。如果您更正了这些属性,它应该可以正常工作