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 - Ebean enhancement ignores a model -

ubuntu - How to disable Kernel Module Signing in linux -

SQL php on different pages to Insert (mysqli) -