Object.create() VS functions

Michaël Rouges michael.rouges at gmail.com
Sat Oct 26 07:44:53 PDT 2013


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131026/e1005e84/attachment.html>


More information about the es-discuss mailing list