Bonjour à tous,
`Knowing that every function is an object, I am surprised that the
Object.create() method doesn't really allow cloning function.
So I made an implementation as follows:
`Object.create = (function () {
'use strict';
var slice,
curry,
getOwnPropertyNames,
getOwnPropertyDescriptor,
defineProperty,
onProperty,
onDescriptor,
create;
slice = (function () {
var method;
method = [].slice;
return method.call.bind(method);
}());
curry = function curry(fn) {
var args;
args = slice(arguments, 1);
return function() {
return fn.apply(
this,
args.concat(
slice(arguments)
)
);
};
};
getOwnPropertyNames = curry(Object.getOwnPropertyNames);
getOwnPropertyDescriptor = curry(Object.getOwnPropertyDescriptor);
defineProperty = curry(Object.defineProperty);
onProperty = function onProperty(prototype, property) {
var descriptor;
descriptor = getOwnPropertyDescriptor(this, property);
if (descriptor === undefined || descriptor.writable ||
descriptor.configurable) {
defineProperty(this, property,
getOwnPropertyDescriptor(prototype, property));
}
};
onDescriptor = function onDescriptor(propertyDescriptors, property) {
defineProperty(this, property, propertyDescriptors[property]);
};
create = function create(prototype, propertyDescriptors) {
var returnValue, onProtoProperty, onPropertyDescriptor;
returnValue = prototype instanceof Function
? curry(prototype)
: {};
getOwnPropertyNames(prototype)
.forEach(onProperty.bind(returnValue, prototype));
if (typeof propertyDescriptors === 'object') {
getOwnPropertyNames(propertyDescriptors)
.forEach(onDescriptor.bind(returnValue, propertyDescriptors));
}
return returnValue;
};
return create;
}());`
Is there a reason not to do that, please?
Thanks in advance.
Michaël Rouges - https://github.com/Lcfvs - @Lcfvs
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss