访问功能数组中的功能 [英] Access a function within an array of functions
问题描述
创建一个变量,该变量是一个函数,并且在该函数中有一组函数数组,我想访问第三级,但是我不能,给我发送错误,有人认为我做错了吗?
create a variable where is a function and within that function there is a set of array of functions, I want to access the third level but I can not, send me an error, someone has an idea that I am doing wrong?
var mivarobject = function(){
// array functions
helpers = {
// functions
function1 : function(){
return 1;
},
function2 : function(){
return 2;
},
}
}
console.log(mivarobject.helpers.function1());
这样我得到以下错误:未捕获的TypeError:无法读取未定义的属性'function1'
With this I get the following error: Uncaught TypeError: Cannot read property 'function1' of undefined
推荐答案
假设您打算使用 mivarobject
作为类,则您缺少 new
关键字。
You are missing the new
keyword, assuming your intention is to use mivarobject
as a class.
此外,您正在全局声明 helpers
。添加 this
将其实例化为 mivarobject
的属性。
Additionally, you're declaring helpers
globally. Add this
to instantiate it as a property of mivarobject
.
var mivarobject = function(){
this.helpers = {
function1: function(){
return 1;
}
};
};
console.log((new mivarobject).helpers.function1());
这篇关于访问功能数组中的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!