javascript - Is there a way to declare functions in constructor? -
first of all, recognize there 100+ threads on method.this vs var method, etc. not asking. know differences. confused of function expressions vs function declarations in context of creating objects using constructor.
what wanted know this: how come function declarations never appear in constructors, function expressions do? whenever declare function rather use expression not accessible in object once initiated; why this?
i think because either local (aka private) , inaccessible, or when creating objects, if constructor not see function prefixed 'this', 'var', or 'constructor.prototype', skips it.
how come function declarations never appear in constructors, function expressions do?
they do
whenever declare function rather use expression not accessible in object once initiated; why this?
you must doing wrong.
function thing() { function go() { alert('hello'); } this.foo = 1; this.bar = function () { go(); } } var x = new thing(); x.bar();
i think because either local (aka private) , inaccessible
function declarations locally scoped. doesn't stop assigning them wider scope, object property or using them within scope declared within.
Comments
Post a Comment