JSFiddle - React, Tailwind, and code Playground
by xavierm02
HTML
<div id="console"></div>
JavaScript
// Implement what I would like
Object.defineProperties = ( function ( ) {
var defineProperties = Object.defineProperties;
return function ( object, propertyDescriptors ) {
for ( var name in propertyDescriptors ) {
if ( propertyDescriptors.hasOwnProperty( name ) ) {
( function ( propertyDescriptor ) {
var get;
var set;
if ( propertyDescriptor.hasOwnProperty( 'get' ) ) {
get = propertyDescriptor.get;
propertyDescriptor.get = function ( ) {
return get.call( this, propertyDescriptor.value );
};
}
if ( propertyDescriptor.hasOwnProperty( 'set' ) ) {
set = propertyDescriptor.set;
propertyDescriptor.set = function ( value ) {
return propertyDescriptor.value = set.call( this, value );
};
}
} )( propertyDescriptors[ name ] );
}
}
defineProperties( object, propertyDescriptors );
};
} )( );
// what I think it should look like
function Point( x, y ) {
this.x = x;
this.y = y;
}
Object.defineProperties( Point.prototype, {
x: {
get: function ( value ) {
return value;
},
set: function ( value ) {
value = parseInt( value );
return value;
}
},
y: {
// same here
}
} );
// code used for testing
var c = document.getElementById( 'console' );
function log( m ) {
c.appendChild( document.createTextNode( m ) );
c.appendChild( document.createElement( 'br' ) );
}
function assert( b, m ) {
if ( !b ) {
log( m );
}
}
// tests
try {
var p = new Point( 1, 2 );
assert( p.x === 1, 'bad x 1' );
p.x = 2;
assert( p.x === 2, 'bad x 2' );
p.x = '3';
assert(...