7362

Default hash values

Question:

I'd like to have a hash with params members with defaults default values:

var defaults = { item1: "def1", item2: "def2" }; var params = { item2: "param2", item3: "param3" }; var result = // some clever code here... console.log(result); // { item1: "def1", item2: "param2", item3: "param3" };

The most clever code I can figure out is to iterate defaults members and add them into params if they are missing there. I wonder if there is some native solution instead of writing own code? Prototyping seems promising, but it only works with functional objects, which I don't want here. Any thoughts?

Answer1:

If anyone is still interested: after almost two years, I guess the clever code mentioned in the question would be

__proto__: Object.create(defaults)

See the example:

var defaults = { item1: "def1", item2: "def2" }; var params = { item2: "param2", item3: "param3", __proto__: Object.create(defaults) }; for(var i in params) console.log(i,params[i]); // item2 param2 // item3 param3 // item1 def1

Just keep in mind that the __proto__ is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto" rel="nofollow">deprecated</a>, however well supported unlike the setPrototypeOf() method suggested by the link. Let's see what the future will be.

Recommend

  • Macro alternative for C++ code generation
  • Why single SQL delete statement will cause deadlock?
  • Access to internal function from externally loaded HTML in React component
  • How to simplify repeated build job syntax?
  • How does the heap manager in java or C++ keep track of all the memory locations used by the threads
  • Refreshing i18n translated string interpolated values in Aurelia
  • how to UNSELECT row in a ttk.Treeview in tkinter
  • True privateness in Python
  • UML diagram generator in Visual Studio 2010
  • Embedded Glassfish JPA Datasource connection fail
  • cell spacing in div table
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Custom Tabgroup Appcelerator
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • Is there a amazon webstore API for customers?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Perl system calls when running as another user using sudo
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • How to pass list parameters for each object using Spring MVC?
  • RestKit - RKRequestDelegate does not exist
  • Numpy divide by zero. Why?
  • php design question - will a Helper help here?
  • Traverse Array and Display in markup
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Acquiring multiple attributes from .xml file in c#
  • Why joiner is not used after Sequence generator or Update statergy
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • Is it possible to post an object from jquery to bottle.py?
  • java string with new operator and a literal