Object.create() VS functions

Nathan Wall nathan.wall at live.com
Sat Oct 26 09:01:42 PDT 2013


Before getting into the exact situation with `Object.create` and functions, the first thing to understand is that `Object.create(proto)` creates an object with prototype of `proto`.  As others have said, this is different from cloning.

Here's a simple example which should show the differences between the built-in `Object.create` and your version which creates a clone.

    var A = { foo: 1 },
        B = Object.create(A);

    A.foo = 7;
    console.log(B.foo); // => 7

With your version of `Object.create`, the last line would log `1` instead of `7`.  With built-in `Object.create` it's `7` because `A` is the prototype of `B`, so on property access all own properties of `B` are checked first and if a match isn't found then the properties of `A` are checked.

To continue the above example:

    B.foo = 8;
    console.log(B.foo); // => 8

    delete B.foo;
    console.log(B.foo); // => 7

Make sense?

Nathan



________________________________
> Date: Sat, 26 Oct 2013 16:44:53 +0200 
> Subject: Object.create() VS functions 
> From: michael.rouges at gmail.com 
> To: es-discuss at mozilla.org 
> 
> 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 
> es-discuss at mozilla.org https://mail.mozilla.org/listinfo/es-discuss 		 	   		  


More information about the es-discuss mailing list