What are the benefits of making properties non-enumerable?

I think the main benefit is to be able to control what shows up when enumerating an object’s properties, such as for in or Object.keys(). MDN explains it well with Object.defineProperty: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty So normally, when people want to add a method to Object, such as a polyfill for some method not supported in old browsers, … Read more

IEnumerable doesn’t have a Count method

You add: using System.Linq; at the top of your source and make sure you’ve got a reference to the System.Core assembly. Count() is an extension method provided by the System.Linq.Enumerable static class for LINQ to Objects, and System.Linq.Queryable for LINQ to SQL and other out-of-process providers. EDIT: In fact, using Count() here is relatively inefficient … Read more

Group hashes by keys and sum the values

ar = [{“Vegetable”=>10}, {“Vegetable”=>5}, {“Dry Goods”=>3}, {“Dry Goods”=>2}] p ar.inject{|memo, el| memo.merge( el ){|k, old_v, new_v| old_v + new_v}} #=> {“Vegetable”=>15, “Dry Goods”=>5} Hash.merge with a block runs the block when it finds a duplicate; inject without a initial memo treats the first element of the array as memo, which is fine here.

What does enumerable mean?

An enumerable property is one that can be included in and visited during for..in loops (or a similar iteration of properties, like Object.keys()). If a property isn’t identified as enumerable, the loop will ignore that it’s within the object. var obj = { key: ‘val’ }; console.log(‘toString’ in obj); // true console.log(typeof obj.toString); // “function” … Read more

Array#each vs. Array#map

Array#each executes the given block for each element of the array, then returns the array itself. Array#map also executes the given block for each element of the array, but returns a new array whose values are the return values of each iteration of the block. Example: assume you have an array defined thusly: arr = … Read more

tech