In this example, how would I add a default foobar: function() {}
property to each object inside of Foo
?
Foo = {
bar1: {
foobar: function() {},
width: "10m"
},
bar2: {
foobar: function() {},
width: "20m"
}
}
What if you organized your code a bit more to something like this?
function Bar(width) {
this.foobar = function() {};
this.width = width
}
Foo = {
bar1: new Bar('10m'),
bar2: new Bar('20m'),
bar3: new Bar() //optional example
}
In this case, every Bar
you create will come with foobar()
as a default function, and you can pass width
as an optional parameter.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments