object - Returning properties on a Javascript Constructor function -
i trying solve problem have following piece of code:
var piggie = new animal(animal.pig);
how can constructor function (new animal) object properties (animal.pig)?
i have tried solution:
function animal(type) { this.typeof = type; return { pig: 'pig' }; }
but animal.pig undefined? js fiddle here: http://jsfiddle.net/bufr2b4c/
for animal.pig
have value, have create property on constructor function itself.
function animal(type) { this.typeof = type; } animal.pig = "pig";
your code creating object when constructor runs , setting pig on that object. (and discarding constructed animal instance go).
Comments
Post a Comment