Object.create() VS functions

David Bruant bruant.d at gmail.com
Sat Oct 26 08:20:31 PDT 2013


Le 26/10/2013 15:44, Michaël Rouges a écrit :
> Bonjour à tous,
Bonjour,

> `Knowing that every function is an object, I am surprised that the 
> Object.create() method doesn't really allow cloning function.
I don't follow the logic of this sentence.
In any case, the purpose of Object.create is to create a normal object, 
that is an object as commonly understood when it comes to its own 
properties (no magic property like array's "length"), without private 
state (like Date objects) and that is not callable.
Also, Object.create does not create a clone, but a new object.

> <implementation>
If you don't care about |this|, f2 = f.bind(undefined) can be considered 
as a way to clone a function.

   function f2(){
     return f.apply(this, arguments);
   }
works too.

   f2 = new Proxy(f, {})
is a form of function cloning as well.

Very much like object cloning, function cloning does not have one unique 
definition.

> Is there a reason not to do that, please?
I would ask the opposite question: is there a reason to do that? Usually 
features are added because there is a driving use case which you haven't 
provided.

Also, usually, changing the semantics of an existing built-in isn't a 
good idea given that it may break existing code relying on it.

Last, if you can implement it yourself, why do you need it to be part of 
the language? There are hundreds of convenience functions that could be 
added. Why this one more than others?

Thanks,

David


More information about the es-discuss mailing list