Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33253

Re: Object.defineProperties

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Object.defineProperties
Date 2017-05-08 00:50 +0200
Organization albasani.net
Message-ID <oeo8ca$huq$1@news.albasani.net> (permalink)
References (6 earlier) <oekitc$2tgv$1@csiph.com> <oelc5o$4iv$1@news.albasani.net> <oeldrk$j52$1@csiph.com> <oelip6$jk1$1@news.albasani.net> <oemkil$1gok$1@csiph.com>

Show all headers | View raw


Cezary Tomczyk wrote:
> So, Object.assign does shallow copy, not deep copy. My goal , by having
> "extend" function in that case, is to do deep copy.
> 
> If I do extend({}, o); then new object will have his own data and changes
> made on new object must not reflect on the source object.

Okay, that makes sense. I thought you were saying that the action of
Object.assign() itself caused the source object to be modified.

>     function extend(target, source) {
>         var output = target;
> 
>         function processProperty(key) {
>             if (isTypeOf(source[key], 'object')) {
>                 output[key] = {};
>                 extend(output[key], source[key]);
>             } else if (isTypeOf(source[key], 'array')) {
>                 output[key] = source[key].slice(0);
>             } else {
>                 output[key] = source[key];
>             }
>         }
> 
>         Object.keys(source).forEach(processProperty);
> 
>         return output;
>     }

Without testing this myself, I can see a potential problem: an array in the
source object could contain objects as elements. Your extend() function does
a shallow copy on arrays, so modifying objects in the target array will
affect both the source and the target.

Apart from that, I still don't quite see the use for the isTypeOf()
function... what's wrong with this:

    if (Array.isArray(source[key])) {
        // clone array (and recurse)
    } else if (typeof source[key] == "object" && source[key] !== null) {
        // clone object (and recurse)
    } else {
        // copy scalar values (and null)
    }

For the sake of completeness, I'd like to amend my earlier statement:
The `typeof` operator, `Array.isArray()`, *and strict comparisons* are
usually all that's needed.

Also for the sake of completeness, not all objects can be cloned 1:1 so that
the they are 100% independent and disconnected. Simple data container
objects should be fine, though.

> However, when I run it in PhantomJS 2.1.1 (Mac OS X) then it throws exception:
> 
> "TypeError: Requested keys of a value that is not an object. in object.js
> (line 9)"
> 
> While I understand that "source" is passed as a non-object then I can't find
> out what's causing it. At least debugging PhantomJS is a bit hard.

Sorry, I don't have enough experience with PhantomJS to help with that. I
suggested it for my current project, but was rejected. Their mailing list
may be worth a try: https://groups.google.com/forum/#!forum/phantomjs


- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 08:36 +0200
  Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 10:14 +0200
    Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 10:59 +0200
      Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 12:36 +0200
        Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-03 18:47 +0200
          Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:18 +0200
    Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:14 +0200
      Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-05 19:37 +0200
        Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:11 +0200
          Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 15:26 +0200
            Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 16:43 +0200
              Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 20:23 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 22:21 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 23:19 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 02:40 +0200
            Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-06 22:37 +0200
              Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-06 23:05 +0200
                Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-07 00:29 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 10:06 +0200
                Re: Object.defineProperties Stefan Weiss <krewecherl@gmail.com> - 2017-05-08 00:50 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-08 13:17 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 02:37 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 09:42 +0200
                Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-07 17:11 +0200
                Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-07 20:08 +0200
        Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:59 +0200
          Re: Object.defineProperties John G Harris <niam@jghnorth.org.uk.invalid> - 2017-05-06 10:35 +0100
  Re: Object.defineProperties Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-05-05 08:32 +0200
  Re: Object.defineProperties Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 18:24 +0200

csiph-web