Your first option doesn't use object notation. If you want to use object notation, you could write something like this:
function MyLittleHouse(name) {
var age = 88;
age += 20;
return {
getName: function() {
return name;
}
}
}
This has the advantage of not using this
, which means you avoid any issues with the binding of this
. It also hides age
and name
, which may or may not be desirable -- as it stands, there's no way for age
to be accessed, while name
is immutable since it can only be read via getName
. If you want to expose age
as a normal field:
function MyLittleHouse(name) {
var age = 88;
age += 20;
return {
age: age,
getName: function() {
return name;
}
}
}
Michael Williamson
2010-09-10 10:49:27