Greetings, there is an object and I need to create on the basis of it another 3 by passing different parameters to them, respectively, these objects will also be output by different parameters.

Here is my non-working example .

var names = ['Вася','Петя','Гриша'], objs = []; var foo = { bar : function(name){ this.name = name; }, baz : function(){ console.log('Ваше ммя: ' + this.name); } }; for(var i in names) objs[ i ] = new foo; objs[ i ].bar( names[ i ] ); for(i in objs) $('#names').append('<li>' + objs[ i ].baz() + '</li>'); 

Closed due to the fact that off-topic participants Pavel Mayorov , aleksandr barakin , insolor , user194374, Denis Aug 22 '16 at 6:12 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Pavel Mayorov, insolor, Spirit of the community, Denis
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And should not work. Where did you see such a method? - Pavel Mayorov
  • @PavelMayorov, I wrote an example that I essentially need. How can I solve it with built-in classes? Can you tell something? Thank you - Vladislav Siroshtan
  • one
  • @PavelMayorov, thanks a lot, read it and everything turned out. - Vladislav Siroshtan

1 answer 1

With the help of Pavel Mayorov , who provided a link to the Mozilla resource , I solved my problem.

Here is the following code :

 var names = ['Вася','Петя','Гриша'], objs = []; var foo = { bar : function(name){ this.name = name; }, baz : function(){ return 'Ваше ммя: ' + this.name; } }; for(var i in names){ objs[ i ] = Object.create( foo, { name : { value: names[ i ] } }); } for(var i in objs) //console.log( objs[ i ].baz() ); $('#names').append('<li>' + objs[ i ].baz() + '</li>');