javascript - Underscore.js script not rendering in Node? -


noob js , underscore --

in following simple code trying iterate through var animalnames using underscore (_.each fxn) when run code in node in terminal goes right cmd prompt ie. nothing runs. please explain what's going.

function animalmaker(name) {     return { speak: function () { console.log("my name ", name); } }; }; var animalnames = ['', '', ''];  var farm = [];  us.each(animalnames, function (name) { farm.push(animalmaker(name)); }); 

firstly, map method more appropriate in case, because you're mapping array of names array of animals

// renamed animalmaker // in js it's convention capitalize constructors , keep normal functions camel-case var createanimal = function(name) {     return {         speak: function() { console.log("my name is", name); }     }; };  var names = ["chicken", "cow", "pig"]; var animals = us.map(names, createanimal); 

with code you've created list of animals. still need make animals speak:

us.each(animals, function(animal) {     animal.speak(); }); 

or use invoke call method each object in list:

us.invoke(animals, "speak"); 

without underscore (native javascript in node.js) write:

var animals = names.map(createanimal); animals.foreach(function(animal) {     animal.speak(); }); 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -