JSFiddle - React, Tailwind, and code Playground
by finom
JavaScript
Object.defineProperties( Object.prototype, {
define: {
value: function( key, descriptor ) {
if( descriptor ) {
Object.defineProperty( this, key, descriptor );
} else {
Object.defineProperties( this, key );
}
},
enumerable: false
},
extendNotEnum: {
value: function( key, property ) {
if( property ) {
this.define( key, {
value: property,
enumerable: false,
configurable: true
});
} else {
for( var prop in key ) if( key.hasOwnProperty( prop ) ){
this.extendNotEnum( prop, key[ prop ] );
}
}
},
enumerable: false
}
});
Object.prototype.extendNotEnum({
extend: function( o ) {
for( var i in o ) {
this[ i ] = o[ i ]
}
}
});
Function.prototype.extendNotEnum( 'inherits', function( Parent ) {
var prototype = this.prototype;
this.prototype = Object.create( Parent.prototype );
this.prototype.constructor = this;
this.prototype.extend( prototype );
});
Widget = function( tag, html ) {
this.element = document.createElement( tag );
this.element.innerHTML = html;
document.body.appendChild( this.element );
};
Button = function() {
Widget.call( this, 'button', 'Кнопка' );
console.log( this )
this.element.addEventListener( 'click', this.action.bind( this ) );
};
Button.prototype.action = function() {
alert( 'Default Action Method' )
};
Button.inherits( Widget ); // смысла здесь мало, так как прототип виджета пустой
GreenButton = function() {
Button.call( this );
this.colors = { odd: 'green', even: 'red' };
this.element.style.backgroundColor = this.colors.odd;
this.clicked = 0;
}
GreenButton.prototype = {
action: function() {
Button.prototype.action.call( this );
...