JSFiddle - React, Tailwind, and code Playground

by franklinjavier

JavaScript

function PointBad(x, y) {
    if ( !x ) {
        x = 320;
    }
    if ( !y ) {
        y = 240;
    }
    return { x: x, y: y };
}

function PointGood( x, y ) { 
    this.x = x || 320;
    this.y = y || 240;
    
    return { x: this.x, y: this.y };
}

function PointBest(x, y) {
    if (typeof x === "undefined") {
        x = 320;
    }
    if (typeof y === "undefined") {
        y = 240;
    }
    return { x: x, y: y };
}


console.log(PointBad(0, 0)); // Err { x: 320, y: 240 }
console.log(PointGood(0, 0)); // Err { x: 320, y: 240 } 
console.log(PointGood()); //  { x: 320, y: 240 }
console.log(PointBest(0, 0)); // { x: 0, y: 0 }
console.log(PointBest()); // { x: 320, y: 240 }