[英]Nesting Objects in Javascript - Anonymous is not a function error
Ok, so I'm a complete newbie to OOP in Javascript, apparently. 好的,显然,我是Java OOP的完全新手。 I thought I understood it, but it appears I only know a small portion.
我以为我理解了,但看来我只知道一小部分。 Anyway, what I'm trying to do is setup an object to store and return data from an XML input by using a fairly simple string to retrieve data.
无论如何,我正在尝试通过使用一个相当简单的字符串来检索数据来设置一个对象来存储和返回XML输入中的数据。 I'd like to retrieve the data with a string similar to
reader.getItem().getSubItem()
or something like that. 我想使用类似于
reader.getItem().getSubItem()
类的字符串来检索数据。
Below is an example of what I attempted, but I get the error anonymous is not a function
each time I try to do a call to fr.getType().isTexture()
so obviously, I need to change something. 下面是我尝试的示例,但是每次尝试调用
fr.getType().isTexture()
,我都得到anonymous is not a function
错误anonymous is not a function
,因此很明显,我需要更改某些内容。
//Create the object by passing an XML element containing sub-elements
var fr = new FeatureReader(test.child(i));
alert(fr.getName()); //returns the object's name
alert(fr.getType().isTexture()); //"anonymous is not a function" error
function FeatureReader(feature) {
var feat = feature;
this.getName = function() {
return feat.name;
};
this.getType = new function() {
this.isTexture = new function() {
if (feat.type.texture == "yes") {
return true;
}
return false;
};
this.isModel = new function() {
if (feat.type.model == "yes") {
return true;
}
return false;
};
};
}
Now, obviously I could just remove the surrounding this.getType = function() {}
around the this.isTexture
and this.isModel
to get my data, but for the sake of learning something, I'd like to see how it is recommended that I set this object up to get the returned values using a string similar to what I mentioned in the first and second paragraphs. 现在,显然,我可以删除
this.isTexture
和this.isModel
周围的this.isTexture
this.getType = function() {}
以获取数据,但是为了学习一些知识,我想看看如何推荐它我使用与第一和第二段中提到的字符串相似的字符串来设置此对象以获取返回值。
When you do this: 执行此操作时:
this.isTexture = new function() {
if (feat.type.texture == "yes") {
return true;
}
return false;
};
you're setting the "isTexture" property to the object constructed, not to that function. 您将“ isTexture”属性设置为构造的对象,而不是该函数。 If you drop the
new
keyword from the statement, you'll be setting "isTexture" to be a function. 如果从语句中删除
new
关键字,则将“ isTexture”设置为函数。
An expression of the form new <some-function>
evaluates to an object, in other words. 换句话说,形式为
new <some-function>
的表达式对一个对象求值。
edit — your "getType" property will also be an object, for the same reason. 编辑 -出于相同的原因,您的“ getType”属性也将是一个对象。 However, I think this would work:
但是,我认为这会起作用:
alert( fr.getType.isTexture() );
Also note that your if
statement can be simplified: 还要注意,您的
if
语句可以简化:
return feat.type.texture == "yes";
What you can do is simply assign an object instead of using new
: 您可以做的就是简单地分配一个对象,而不是使用
new
:
function FeatureReader(feature) {
var feat = feature;
this.getName = function() {
return feat.name;
};
this.getType = {
isTexture: function() {
return feat.type.texture == "yes";
},
isModel: function() {
return feat.type.model == "yes";
}
};
}
Then use the method like: 然后使用如下方法:
instance.getType.isTexture()
Note that you don't need to return true
or false
, as returning an expression that evaluates to boolean like a == b
returns a boolean value. 请注意,您不需要返回
true
或false
,因为返回的表达式的计算结果类似于a == b
的布尔值,则返回布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.