JavaScript Set *any* property of an object to a default value

I was wondering if it is possible to override "undefined" for uninitiated keys of an object such that:

var obj={}
console.log(obj.randomKey)

output in console:
0

I am well aware that I "forgot" to initate obj.randomKey. That is in fact my question.

Can I have a default value for any uninitiated key of an object or a class?

If you are curious

The reason I am asking is that I am trying to solve a riddle that allows creation of object with "native" language such as:

const jane = new Thing('Jane')
jane.name // => 'Jane'    

// can define boolean methods on an instance
jane.is_a.person
jane.is_a.woman
jane.is_not_a.man

jane.is_a_person // => true
jane.is_a_man // => false

And this is the first strategy I am taking :)

Update: I solved the riddle :) If anyone's interested in trying: https://www.codewars.com/kata/the-builder-of-things/train/javascript

Answer:1

You can't really define a default value, but you can use ES2015 proxies to pretend that they have one:

function createObj(obj) {
  return new Proxy(obj, {
    get(target, key, receiver) {
      if (target[key] === undefined) {
        return 0;
      }

      return Reflect.get(target, key, receiver);
    }
  });
}

const myObj = createObj({});
console.log(myObj.test); // 0

Nice approach to a language btw!

Answer:2

I want to create 4 <span> tags and add different text to them. for (var i = 0; i <= 4; i++) { var spanElement = document.createElement("span") + i; var value_of_span += uniques + i; ...

I want to create 4 <span> tags and add different text to them. for (var i = 0; i <= 4; i++) { var spanElement = document.createElement("span") + i; var value_of_span += uniques + i; ...

  1. create multiple tags javascript
  2. create multiple tags in wordpress
  3. create multiple tags
  4. git create multiple tags
  5. docker create multiple tags
  6. sharepoint create multiple tags
  7. aws cli create multiple tags
  8. sublime text create multiple tags

I'm using Element UI and things have changed since the release of Vue.js 2.3 I have a dialog that should be displayed only if the following condition is met private.userCanManageUsers && ...

I'm using Element UI and things have changed since the release of Vue.js 2.3 I have a dialog that should be displayed only if the following condition is met private.userCanManageUsers && ...

I'm new to javascript and cannot understand simple thing - what is the difference between ...(x) => { return x*2} and ...x => { return x*2} //(just for example, may not work) Can someone ...

I'm new to javascript and cannot understand simple thing - what is the difference between ...(x) => { return x*2} and ...x => { return x*2} //(just for example, may not work) Can someone ...

  1. ecmascript arrow functions
  2. ecmascript arrow function syntax
  3. ecmascript 6 arrow functions
  4. ecmascript 5 arrow functions
  5. ecmascript 2015 arrow function
  6. ecmascript class arrow function
  7. ecmascript 6 foreach arrow functions

I want to automatically fill out the div with id= id, name, email, company.. after a click on any search result. The id from the search result is used as filter to get the appropriate row from Mysql ...

I want to automatically fill out the div with id= id, name, email, company.. after a click on any search result. The id from the search result is used as filter to get the appropriate row from Mysql ...

  1. dynamic-link library search order