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

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -