Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34598
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2018-01-18 20:06 -0800 |
| References | (2 earlier) <6a8311a3-4cd1-4d3d-b127-b7295255d85d@googlegroups.com> <31fb9b36-354d-44da-89eb-b84551977bc2@googlegroups.com> <f576b7ec-3d71-42f3-89df-1c7126d2a898@googlegroups.com> <1fa87bbc-8c47-4290-8757-a3de5ce828b6@googlegroups.com> <2363754.U4In554KqX@PointedEars.de> |
| Message-ID | <4816b388-10b7-445b-ae2f-040cef723041@googlegroups.com> (permalink) |
| Subject | Re: Get the object structure correct. |
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
Den fredag 19 januari 2018 kl. 04:56:09 UTC+1 skrev Thomas 'PointedEars' Lahn:
> Michael Haufe (TNO) wrote:
>
> > Jonas Thörnvall wrote:
> >> var People = [
> >> {Name: "Name", Surname: "Surname"},
> >> {Name:"AAA", Surname:"ZZZ"},
> >> {Name: "Name", Surname: "AAA"}
> >> ];
> >> I guess people is an array holding three indexes above?
> >> The properties of people is Name and Surname
> >
> > JavaScript developers have a tendency to "golf" [1] their code just like
> > Perl programmers. Which can make things unnecessarily terse.
>
> But often also less error-prone. YMMV.
>
> > I would have wrote the example like this:
> >
> > </script>
> > class Person {
> > constructor(name, surName) {
> > this.name = name;
> > this.surName = surName
> > }
> > }
> >
> > var people = [
> > new Person(Brendan", "Eich"),
> > new Person("Alan","Kay"),
> > new Person("Robert", "Carlyle")
> > ]
>
> Names starting with a capital letter should be reserved for constructors,
> classes and constants That is, “People”, “Name”, and “Surname” should all
> be lowercase here.
>
> Using the same pattern as you used for “tracks”, one can set up the data and
> have the custom instances without repeating oneself (ES 2015+):
>
> people = people.map((obj) => new Person(obj.name, obj.surname));
>
> or, more backwards-compatible (ES Ed. 5+):
>
> people = people.map(function (obj) {
> return new Person(obj.name, obj.surname);
> });
>
> Or, most backwards-compatible (all):
>
> for (var i = people.length; i--;)
> {
> var obj = people[i];
> people[i] = new Person(obj.name, obj.surname);
> };
>
> If the class/type has a default constructor (and this is one example that
> show that it is a good idea to have one), and provides means to set
> individual properties, then this can be even simpler. For example:
>
> people = people.map((obj) => {
> var person = new Person();
> Object.assign(person, obj);
> });
>
> It is also a good idea to have a constructor that in addition to positional
> arguments accepts an initializer object as first and only argument.
>
> people = people.map((obj) => new Person(obj));
>
> Such a constructor is particularly useful with JSON.parse(json, reviver),
> and in general it is a good idea if a constructor can be called as a
> function for type conversion (like type casting). But apparently this is
> not possible with ECMAScript classes:
>
> class Person
> {
> constructor (...args)
> {
> if (!(this instanceof Person)) return new Person(...args);
>
> /* initialize */
> }
> }
>
> /* TypeError: Class constructor Person cannot be invoked without 'new' */
> Person()
>
> This works:
>
> function Person (...args)
> {
> /*
> * For backwards compatibility JSX:object.js introduces
> * Function.prototype.construct(), and I am also thinking about
> * adding Function.prototype.toFactory() for DRY.
> */
> if (!(this instanceof Person)) return new Person(...args);
>
> /* initialize */
> }
>
> /* > Person {} */
> Person()
>
> BTW, one should have read <http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/>.
>
> --
> PointedEars
> FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
I like the old version Thomas
for (var i = people.length; i--;)
{
var obj = people[i];
people[i] = new Person(obj.name, obj.surname);
};
But if Person was initself a property of adress would i then write as below
to create a new person object on the adress. It is the syntax that escapes me.
for (var i = people.length; i--;)
{
var obj = people[i];
residence[x].people[i] = new Person(obj.name, obj.surname);
};
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 02:50 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 03:03 -0800
Re: Get the object structure correct. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-01-18 14:39 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 16:38 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 16:56 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 17:19 -0800
Re: Get the object structure correct. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-01-18 17:33 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 19:54 -0800
Re: Get the object structure correct. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-01-19 04:55 +0100
Re: Get the object structure correct. "Mark-" <nospam@mapson.xyz> - 2018-01-19 04:03 +0000
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 20:06 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 20:30 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 20:49 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-19 00:12 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-19 02:01 -0800
Re: Get the object structure correct. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-01-25 07:49 +0100
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-30 01:06 -0800
Re: Get the object structure correct. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-01-30 11:01 +0100
Re: Get the object structure correct. "Mark-" <nospam@mapson.xyz> - 2018-01-30 13:37 +0000
Re: Get the object structure correct. John G Harris <niam@jghnorth.org.uk.invalid> - 2018-01-30 19:14 +0000
Re: Get the object structure correct. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-01-18 17:24 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 17:41 -0800
Re: Get the object structure correct. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-01-18 17:47 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 18:29 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 18:46 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 18:54 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-18 19:00 -0800
Re: Get the object structure correct. John G Harris <niam@jghnorth.org.uk.invalid> - 2018-01-19 11:20 +0000
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-19 11:48 -0800
Re: Get the object structure correct. John G Harris <niam@jghnorth.org.uk.invalid> - 2018-01-19 20:03 +0000
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-19 12:18 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-19 12:48 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-19 13:02 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-20 18:07 -0800
Re: Get the object structure correct. Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-01-21 00:55 -0800
csiph-web