Object DefineProperties method

Object DefineProperties method
0

#1

Hello ,campers ,I don’t understand why in the output we have 42 not an other nested object

   const object1 = {};

Object.defineProperties(object1, {
  property1: {
    value: 42,
    writable: true
  },
  property2: {}
});

console.log(object1.property1);
// expected output: 42

can anyone explain to me more this issue .


#2

Hey mustaphason, how is it going?

Well, it’s returning 42 and not an object because on the value of property1 you declared an int, so property1 is an int, not an object

It will return an object if the value is an object such as:

Object.defineProperties(object1, {
  property1: {
    value:{
            question: 'answer for life universe and everything',
            answer: 42
    },
    writable: true
  },
  property2: {}
});

Now it will return an object because now property1 is an object. Got it?


#3

Thank you pedrobslisboa ,to understand your explanation I should know what int is , I searched to know it but I did not find anything ,so could please give me a small info about it .


#4

Sorry, int means integer. I’ve used it to be specific but I could use the word “number” instead.

Hope you understood.

Any doubt, just ask.

See ya

EDIT: It’s cool to know what is infos such as int and float, because js has functions based on this, such as parseInt() and parseFloat(), those are very nice to use.