A small question for versed javascript programmers: There are two ways to declare a function in javascript.
A: javascript-object-notation
function MyLittleHouseA(name) {
this.age = 88,
this.name = name,
this.getName = function()
{
return this.name;
}
}
B: uhm, normal:
function MyLittleHouseB(name) {
this.age = 88;
this.name = name;
//do some more proceduaral stuff upon 'construction'
this.age = this.age + 20;
this.getName = function()
{
return this.name;
}
}
I find A more elegant (and have a number of objects I want to turn into configurable options…), but might want to do some more stuff upon instance creation, hence I do need as shown in B.
Can these be mixed?
¡ Thanx !
Your first option doesn’t use object notation. If you want to use object notation, you could write something like this:
This has the advantage of not using
this, which means you avoid any issues with the binding ofthis. It also hidesageandname, which may or may not be desirable — as it stands, there’s no way forageto be accessed, whilenameis immutable since it can only be read viagetName. If you want to exposeageas a normal field: