[英]Javascript reference error - can't find a function inside object
var model = {
test:function(){console.log("hello")},
show: test() //ReferenceError: test is not defined
}
I want to have a key inside model object to hold a function. 我想在模型对象内部有一个键来保存函数。 Not sure why it gets undefined error.
不知道为什么会得到未定义的错误。
Hope it helps . 希望能帮助到你 。
var model = { test: function() { alert("hello") }, show: function() { return this.test(); } } model.test(); model.show();
use key show as a function, return test from it. 使用key show作为函数,从中返回测试。
I know this is a duplicate but I can't find a good one. 我知道这是重复的,但找不到合适的。 You have to do something like this:
您必须执行以下操作:
var model = {
test:function(){console.log("hello")},
};
model.show = model.test();
If you want to make it less ... primitive, I guess, you can write a constructor function to encapsulate that stuff, or perhaps a "factory"-like function to do so. 我想,如果您想减少它的原始性,您可以编写一个构造函数来封装这些东西,或者编写一个类似于“工厂”的函数来封装它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.