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

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

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

java - Cannot secure connection using TLS -