admin管理员组文章数量:1431708
I would like to know if it is possible to do something like this:
if (mb == null || typeof (mb) != "object") {
var mb = new Object();
}
mb = {
tests: {
onAnimals: {
test: function() {
return "";
}
}
onHumans: {
test: function() {
return "";
}
}
}
}
Bu when I tries it, I can see Tests, but when I dot further in, I can't se onAnimals / onHumans.
javascript is still new to me, so hope you can help.
I would like to know if it is possible to do something like this:
if (mb == null || typeof (mb) != "object") {
var mb = new Object();
}
mb = {
tests: {
onAnimals: {
test: function() {
return "";
}
}
onHumans: {
test: function() {
return "";
}
}
}
}
Bu when I tries it, I can see Tests, but when I dot further in, I can't se onAnimals / onHumans.
javascript is still new to me, so hope you can help.
Share Improve this question edited Dec 21, 2009 at 7:35 Cheeso 193k106 gold badges485 silver badges734 bronze badges asked Dec 3, 2009 at 15:35 JoshloJoshlo 7944 gold badges9 silver badges26 bronze badges5 Answers
Reset to default 7You're missing a ma before onHumans. I've assumed mb to be a global variable; you can use var instead if that's what you need. Also, it's easier to read if you structure it differently, like this:
window.mb = window.mb || {};
window.mb = {
tests: {
onAnimals: {
test: function(){
return "";
}
},
onHumans: {
test: function(){
return "";
}
}
}
};
You are missing a ma before onHumans
if (mb == null || typeof (mb) != "object") {
var mb = new Object();
}
mb = {
tests: {
onAnimals: {
test: function() {
return "animal";
}
},
onHumans: {
test: function() {
return "human";
}
}
}
}
alert(mb); //[object Object]
alert(mb.tests); //[object Object]
alert(mb.tests.onAnimals); //[object Object]
alert(mb.tests.onHumans); //[object Object]
alert(mb.tests.onAnimals.test()); //animal
alert(mb.tests.onHumans.test()); //human
You don't need to declare the variable as an object beforehand, simply using the brackets like that is all you need. You do have a syntax error, missing a mas before "onHumans", but aside from that, it looks good to me. You should be able to reach the functions via mb.tests.onAnimals.test
and mb.tests.onHumans.test
Your code is mostly valid, and you don't even have to do the initial if-check. Just type var mb = { ...
, and you'll start set mb
to a new object, regardless of whether it was one before, or undefined, or something else...
What you're missing is a ma after the onAnimals declaration tho:
mb = {
tests: {
onAnimals: {
test: function() {
return "";
}
},
onHumans: {
test: function() {
return "";
}
}
}
}
As stated in some of the ments above testing for the existence of the variable mb is redundant when you just re-declare it to append your methods and properties.
If you are testing for the existence of the variable to add additional methods to the object you should use a method like jQuery's extend or ExtJs's apply method.
jQuery:
window.mb = $.extend(window.mb||{}, {
tests: {
onAnimals: {
test: function() {
return "";
}
},
onHumans: {
test: function() {
return "";
}
}
});
Ext:
window.mb = Ext.apply(window.mb||{}, {
tests: {
onAnimals: {
test: function() {
return "";
}
},
onHumans: {
test: function() {
return "";
}
}
});
This snippet of code will either append the tests object to the existing mb variable or create the mb variable then append the tests object to it.
本文标签: class designJavascript data structuresStack Overflow
版权声明:本文标题:class design - Javascript data structures - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745578619a2664483.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论