JSFiddle - React, Tailwind, and code Playground
HTML
<script>
/*
pd will return all the own propertydescriptors of the object
@param Object obj - object to get pds from.
@return Object - A hash of key/propertyDescriptors
*/
var pd = function _pd(obj) {
var keys = Object.getOwnPropertyNames(obj);
var o = {};
keys.forEach(function _each(key) {
var pd = Object.getOwnPropertyDescriptor(obj, key);
o[key] = pd;
});
return o;
};
/*
Will extend native objects with utility methods
@param Boolean prototypes - flag to indicate whether you want to extend
prototypes as well
*/
pd.extendNatives = function _extendNatives(prototypes) {
if (!Object.getOwnPropertyDescriptors) {
Object.defineProperty(Object, "getOwnPropertyDescriptors", {
value: pd,
configurable: true
});
}
if (!Object.extend) {
Object.defineProperty(Object, "extend", {
value: pd.extend,
configurable: true
});
}
if (!Object.make) {
Object.defineProperty(Object, "make", {
value: pd.make,
configurable: true
});
}
if (!Object.prototype.new && prototypes) {
Object.defineProperty(Object.prototype, "new", {
value: function _new() {
var o = Object.create(this);
o.constructor && o.constructor.apply(o, arguments);
return o;
},
configurable: true
});
}
return pd;
};
/*
Mixin is similar to Extend except it will combine the two constructor
functions to ensure that the mixin is also constructed.
@param Object target - target which the source will be mixed into
@param Object source - the mixin
@return Object - the target
*/
pd.mixin = function _mixin(target, source) {
var constructorTarget = target.constructor,
constructorSource = source.constructor;
pd.extend(target, source);
if (constructorTarget &&...
JavaScript
// https://github.com/Raynos/pd
pd.extendNatives();
var BaseClass = {
parseXML: function () {
alert("base");
},
constructor: function () { this._map = {}; }
};
var ChildClass = Object.make(BaseClass, {
constructor: function () {
BaseClass.constructor.apply(this,arguments);
},
parseXML: function () {
alert("child");
BaseClass.parseXML.apply(this,arguments);
}
});
var a = pd.new(ChildClass);
a.parseXML();