using 1 and 0 instead of true false for Object.defineProperty

61 views Asked by At

I've seen code on the web like this (simplified):

    var abc = {}; 
    Object.defineProperty(abc, 'example', {
        enumerable: 0,
        configurable: 1,
        writable: 1,
        value: function() {}
    });

where in place of true and false they use 1 and 0. I prefer this method for its simplicity, but I need to know:

Is this defining format nonstandard and inconsistent across browsers and should be avoided, or is it safe to use in production-level code?

1

There are 1 answers

1
Tsuina On

It looks shorter in the code you wrote but having boolean is actually faster in many situations. You would have to write

if(writable===1){
...
}

instead of

if(writable){
...
}

same with the use of question marks which I find really nice in some situation:

writable? doThisIfTrue : doThisIfFalse

It's my first answer on the site and I'm a junior developer so I hope it helped!