> noone? JSLint doesn't even let you write a for/in loop if you don't have > `obj.hasOwnProperty(key)` in it, and usually nobody wants inherited > properties (included methods from old classes/ahem prototypes) reassigned > everywhere.
Huh? Old prototypes? Those prototypes have their properties set as non-enumerable since how long now? You yourself said one shouldn’t use Object.assign for objects with behavior in the inheritance chain. And I agree. It’s pretty much only useful for data objects (a.k.a plain). Those have no methods in the inheritance chain one needs to ignore. Again, I ask, what is the problem to which ignoring inherited properties is the solution to? I posit everyone wants inherited properties. Just half of the people have been FUDed into being afraid of inheritance. But arguments based on assumptions on what other people want are irrelevant because naive people are easy to influence. JSLint is how Douglas Crockford writes his code. That’s not an argument for right APIs. JSON.stringify is equally retarded with its inconsistent handling of inheritance, but that’s another matter. And as I said below: until everyone also prefixes their obj.name uses with hasOwn: `obj.hasOwnProperty(“name”) && obj.name`, skipping inherited properties is a fool’s errand. It causes inconsistent APIs because you don’t run your options et alii objects through inheritance strippers every time. And you shouldn’t. Or does anyone disagree with that? This is a bigger problem than my use of Object.assign. Proliferation of Object.assign will prevent everyone else from relying on inheritance. And in a prototypal language I find that bad API design. A. On Feb 27, 2015, at 15:40, Andrea Giammarchi <[email protected]> wrote: > noone? JSLint doesn't even let you write a for/in loop if you don't have > `obj.hasOwnProperty(key)` in it, and usually nobody wants inherited > properties (included methods from old classes/ahem prototypes) reassigned > everywhere. > > If you deal with data objects and dictionaries yuo'll always have a flat > structure, right? If you don't care about properties descriptors (getters and > setters) you can always use a for/in > > ``` > function flatEnumerables(a) { > for (var c, k, i = 1; i < arguments.length; i++) { > for (k in (c = arguments[i])) a[k] = c[k]; > } > return a; > } > ``` > This would do what you are looking for (which is the first time I personally > read/hear about somebody wanting `Object.assign` to behave like a for/in) > > Would that work? > > Best Regards > > > > On Fri, Feb 27, 2015 at 12:56 PM, Andri Möll <[email protected]> wrote: >> You are talking about "flatting" all properties, which is an undesired >> overhead. > > Umm, Object.assign is as it is because of performance? I don’t think it is > nor is that a good reason. Every property access in JavaScript takes > inheritance into account and there are thousands more than a Object.assign > call here and there. > >> But what's bugging me every time more, is that somebody had a very bad idea >> to spread `Object.assign` as something good for inheritance or object >> cloning. >> >> Where does this come from? `Object.assign` retrieves properties and ignore >> getters and setters, is the last tool you want to use as substitution >> principle because it breaks. > > > Ignores getters? You mean merely reads them? That’s not ignoring. Getters are > an implementation detail of an interface. > Prototypes aren't only useful for sharing behavior. They’re just as useful > for data objects and value types. Nothing breaks when you clone or assign > them around. > > Object.assign just read properties from one object and assign them to > another. Something you used to do by hand. It’s just shorter to type > assign(A, B) than type all properties of B out manually. If it’s _not_ meant > to be the function-equivalent of `for (var key in source) target[key] = > source[key]` then that’s too bad as my money is on it’s going to be used as > such. I definitely want to use it for that. > >> But what's bugging me every time more, is that somebody had a very bad idea >> to spread `Object.assign` as something good for inheritance or object >> cloning. > >> Are you dealing with prototypes and `Object.assign` ? You gonna have way >> more problems than a missed flattered structure. >> Can you explain what is your goal ? Wouldn't this work? > > Inheritance? Inheritance is an implementation detail. A function receiving an > object must not care about its inheritance tree as long as it fulfills the > required interface. That’s what the LSP says as well. Even though JavaScript > has no explicit concept of interfaces or types, they’re implicit in any > function call. > > My goal is to save people from having to think every time they call a > function whether this 3rd party function ignores inherited properties or not. > If the callee uses Object.assign, it strips them out, if not, the > dot-operator takes it into account. Surely no-one’s expecting everyone to > prefix _every_ obj.name use with hasOwn: `obj.hasOwnProperty(“name”) && > obj.name`. Why do it in Object.assigns then is beyond me. Inconsistent and > uncalled for. > > A. > > On Feb 27, 2015, at 14:24, Andrea Giammarchi <[email protected]> > wrote: > >> You are talking about "flatting" all properties, which is an undesired >> overhead. >> >> ```js >> >> var b = Object.create( >> Object.getPrototypeOf(a) >> ); >> >> Object.assign(b, a); >> >> ``` >> >> But what's bugging me every time more, is that somebody had a very bad idea >> to spread `Object.assign` as something good for inheritance or object >> cloning. >> >> Where does this come from? `Object.assign` retrieves properties and ignore >> getters and setters, is the last tool you want to use as substitution >> principle because it breaks. >> >> `Object.assign` is good only to define enumerable, writable, and >> configurable properties, like configuration or setup objects. >> >> Are you dealing with prototypes and `Object.assign` ? You gonna have way >> more problems than a missed flattered structure. >> >> Can you explain what is your goal ? Wouldn't this work? >> >> ```js >> >> var a = {}; // or anything else >> >> var b = Object.create( >> Object.getPrototypeOf(a) >> ); >> >> Object.getOwnPropertyNames(a).forEach(function (k) { >> Object.defineProperty(b, k, Object.getOwnPropertyDescriptor(a, k)); >> }); >> >> ``` >> >> >> Best Regards >> >> >> >> On Fri, Feb 27, 2015 at 11:52 AM, Andri Möll <[email protected]> wrote: >> Hello, >> >> Why does Object.assign ignore inherited enumerable properties? >> What is the problem to which ignoring inherited properties is the solution >> to? >> >> All I can see is that it prevents a useful use of inheritance. The Liskov >> substitution principle was mentioned 27 years ago in ’87. Why is >> Object.assign breaking it? >> >> - Everyone who’s messing with Object.prototype has to do it an >> non-enumerable style anyway. >> - Most uses of Object.assign will likely be for objects with a finite number >> of keys. Those form specific and implicit types from which people are likely >> to read with the dot-operator. That takes inheritance into account anyway. >> >> I don’t get the agenda to mess with object inheritance. If one wants methods >> to only be in the parent and data on the child (so Object.assign would only >> copy data), use a classical language. In a delegation-prototypal language >> one should be able to inherit freely because of LSP. >> >> Andri >> >> _______________________________________________ >> es-discuss mailing list >> [email protected] >> https://mail.mozilla.org/listinfo/es-discuss >> >> > >
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

