Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29688 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2016-02-26 22:50 +1100 |
| Last post | 2016-03-08 13:03 -0800 |
| Articles | 20 on this page of 30 — 8 participants |
Back to article view | Back to comp.lang.javascript
Refer object within itself Andrew Poulos <ap_prog@hotmail.com> - 2016-02-26 22:50 +1100
Re: Refer object within itself Scott Sauyet <scott.sauyet@gmail.com> - 2016-02-26 04:51 -0800
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-02-26 14:15 +0100
Re: Refer object within itself "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-02-26 16:43 +0100
Re: Refer object within itself Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-26 19:25 +0100
Re: Refer object within itself Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-02-26 19:23 +0100
Refer object within itself Ram Tobolski <ramtob@gmail.com> - 2016-02-28 10:06 -0800
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-02-28 23:07 +0100
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-02-28 23:23 +0100
Re: Refer object within itself Stefan Weiss <krewecherl@gmail.com> - 2016-02-29 00:11 +0100
Re: Refer object within itself Aleksandro <aleksandro@gmx.com> - 2016-02-29 14:49 -0300
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-02-29 19:49 +0100
Re: Refer object within itself Aleksandro <aleksandro@gmx.com> - 2016-03-01 10:48 -0300
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-01 17:22 +0100
Re: Refer object within itself Aleksandro <aleksandro@gmx.com> - 2016-03-01 15:07 -0300
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-01 20:26 +0100
Re: Refer object within itself Aleksandro <aleksandro@gmx.com> - 2016-03-04 21:53 -0300
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-05 14:22 +0100
Re: Refer object within itself Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-05 10:17 -0800
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-06 11:59 +0100
Re: Refer object within itself Ram Tobolski <ramtob@gmail.com> - 2016-03-06 10:48 -0800
Re: Refer object within itself Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-06 19:07 -0800
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-07 10:44 +0100
Re: Refer object within itself Aleksandro <aleksandro@gmx.com> - 2016-03-07 12:54 -0300
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-07 17:59 +0100
Re: Refer object within itself Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-07 13:44 -0800
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-07 22:59 +0100
Re: Refer object within itself Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-07 17:00 -0800
Re: Refer object within itself "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-08 10:24 +0100
Re: Refer object within itself Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-08 13:03 -0800
Page 1 of 2 [1] 2 Next page →
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2016-02-26 22:50 +1100 |
| Subject | Refer object within itself |
| Message-ID | <76ydnVazm-uPoU3LnZ2dnUU7-TGdnZ2d@westnet.com.au> |
Say I have this object
var foo {
"name":"bar"
"width":600
}
and I want to add another element that references a value within the
object itself. eg
var foo {
"name":"bar"
"width":600,
"height": width * 2
}
Both
"height": this.width * 2
and
"height": foo.width * 2
return errors or undefined.
How do I reference to the object itself within the object?
Or should it be done line this
var foo {
"name":"bar"
"width":600
}
foo.height = foo.width * 2;
Andrew Poulos
[toc] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2016-02-26 04:51 -0800 |
| Message-ID | <66b04eb0-79f7-4abb-9427-03b91405c3ab@googlegroups.com> |
| In reply to | #29688 |
On Friday, February 26, 2016 at 6:50:56 AM UTC-5, Andrew Poulos wrote:
> Say I have this object
>
> var foo {
> "name":"bar"
> "width":600
> }
>
> and I want to add another element that references a value within the
> object itself. eg
>
> var foo {
> "name":"bar"
> "width":600,
> "height": width * 2
> }
One approach:
var foo = (function() {
var width = 600;
return {
name: 'bar',
width: width,
height: 2 * width
}
}());
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-02-26 14:15 +0100 |
| Message-ID | <XnsA5BA90FD53F48eejj99@194.109.6.166> |
| In reply to | #29691 |
Scott Sauyet <scott.sauyet@gmail.com> wrote on 26 Feb 2016 in
comp.lang.javascript:
> On Friday, February 26, 2016 at 6:50:56 AM UTC-5, Andrew Poulos wrote:
>> Say I have this object
>>
>> var foo {
>> "name":"bar"
>> "width":600
>> }
>>
>> and I want to add another element that references a value within the
>> object itself. eg
>>
>> var foo {
>> "name":"bar"
>> "width":600,
>> "height": width * 2
>> }
>
> One approach:
>
> var foo = (function() {
> var width = 600;
> return {
> name: 'bar',
> width: width,
> height: 2 * width
> }
> }());
var foo = { 'name': 'bar','width': 600 };
foo.height = foo.width * 2;
or:
var foo = {};
foo.name = 'bar';
foo.width = 600;
foo.height = foo.width * 2;
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2016-02-26 16:43 +0100 |
| Message-ID | <naprqc$66b$1@solani.org> |
| In reply to | #29688 |
Andrew Poulos wrote:
> Say I have this object
>
> var foo {
> "name":"bar"
> "width":600
> }
>
> and I want to add another element that references a value within the
> object itself. eg
>
> var foo {
> "name":"bar"
> "width":600,
> "height": width * 2
> }
>
> How do I reference to the object itself within the object?
AFAIK, you can't. You may consider to employ a "constructor" function
instead:
function makeFoo(name, width) {
return {name: name, width: width; height: 2 * width};
}
makeFoo("bar", 600);
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-02-26 19:25 +0100 |
| Message-ID | <50246494.NvcPNDuDiy@PointedEars.de> |
| In reply to | #29705 |
Christoph M. Becker wrote:
> Andrew Poulos wrote:
>> and I want to add another element that references a value within the
>> object itself. eg
>>
>> var foo {
>> "name":"bar"
>> "width":600,
>> "height": width * 2
>> }
>>
>> How do I reference to the object itself within the object?
>
> AFAIK, you can't. You may consider to employ a "constructor" function
> instead:
>
> function makeFoo(name, width) {
> return {name: name, width: width; height: 2 * width};
> }
>
> makeFoo("bar", 600);
No, that is not necessary, and maybe also not equivalent to what was
originally intended.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-02-26 19:23 +0100 |
| Message-ID | <10425603.zxHOPjekVl@PointedEars.de> |
| In reply to | #29688 |
Andrew Poulos wrote:
> Say I have this object
>
> var foo {
^
> "name":"bar"
> "width":600
> }
>
> and I want to add another element that references a value within the
> object itself. eg
>
> var foo {
^
> "name":"bar"
> "width":600,
> "height": width * 2
> }
>
> Both
> "height": this.width * 2
> and
> "height": foo.width * 2
>
> return errors or undefined.
They do because “width” has to be resolved *before* the object is
constructed and a reference to it is assigned to “foo” (had you used the
assignment operator). Keep in mind that the above (if written correctly)
is functionally equivalent to:
var o = new Object();
o.name = "bar";
o.width = 600;
/* or this.width or foo.width, respectively */
o.height = width * 2;
foo = o;
> How do I reference to the object itself within the object?
The reason that your approach does not work is that you are _not_ “within
the object” there.
> Or should it be done line this
>
> var foo {
^
> "name":"bar"
> "width":600
> }
> foo.height = foo.width * 2;
Certainly not, as that would be a syntax error as well ;-)
You can write it thus:
var foo = (function () {
var width = 600;
return {
name: "bar"
width: width,
height: width * 2
};
}());
That is not necessarily what you want, because the value of the “height”
property is then not *bound* to the value of the “width” property of the
same object: if the “width” property value changes, the “height” property
value does not change with it.
Without Proxy (ECMAScript 2015+) – see Stefan Ram’s answer –, you can define
this relationship in a more compatible way using a getter (ECMAScript Ed.
5+):
var foo = Object.create(Object.prototype, {
name: {
value: "bar",
configurable: true,
enumerable: true,
writable: true
},
width: {
value: 600,
configurable: true,
enumerable: true,
writable: true
},
height: {
"get": function () {
return this.width * 2;
},
configurable: true,
enumerable: true
}
});
or, less explicit:
var foo = Object.defineProperty(
{
name: "bar",
width: 600
},
"height",
{
"get": function () { return this.width * 2; },
configurable: true,
enumerable: true
}
);
(Getters and Setters have been supported in various ways even before
ECMAScript Ed. 5; see JSX:object.js for a wrapper implementation.)
You can use “null” instead of “Object.prototype” if you do not want your
object to inherit any properties, and you can omit definitions for the
“configurable”, “enumerable”, and “writable” properties if you want the
property to be not configurable, enumerable, or writable, respectively.
Note that such an accessor property with only a getter is read-only, until
you reconfigure it (which is only possible if it is also configurable) to
have a setter (in which case the new getter needs to return, and the new
setter needs to write a value of another property or variable), or to have a
value instead (in which case it also should be defined as writable).
Therefore, another alternative is
var foo = (function () {
var width = 600;
return Object.defineProperties({name: "bar"}, {
width: {
"get": function () { return width; },
"set": function (value) { width = value; },
configurable: true,
enumerable: true
},
height: {
"get": function () { return width * 2; },
configurable: true,
enumerable: true
}
});
}());
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Ram Tobolski <ramtob@gmail.com> |
|---|---|
| Date | 2016-02-28 10:06 -0800 |
| Message-ID | <dfd01b06-7ffd-44e6-94be-7ee2fdab0e18@googlegroups.com> |
| In reply to | #29688 |
Hello, this should work:
var foo = {
"name":"bar",
"width":600,
get height(){return this.width * 2;}
}
console.log(foo.height) // "1200"
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-02-28 23:07 +0100 |
| Message-ID | <XnsA5BCEB469CEDBeejj99@194.109.6.166> |
| In reply to | #29720 |
Ram Tobolski <ramtob@gmail.com> wrote on 28 Feb 2016 in comp.lang.javascript:
> Hello, this should work:
>
> var foo = {
> "name":"bar",
> "width":600,
> get height(){return this.width * 2;}
>}
>
> console.log(foo.height) // "1200"
Well, just "works", but not so nice, imho, is this:
var foo = {
'name':'bar',
'width':600,
get height(){return this.width * 2;}
};
alert(foo.height); // 1200
foo.height = 7;
// Uncaught TypeError: Cannot set property height of #<Object>
// which has only a getter
==========
and also not so nice, imho, is:
var vvv;
var foo = {
get height(){return vvv * 2;}
};
alert(foo.height) ; // NaN
vvv = 20;
alert(foo.height) ; // 40
vvv = 7;
alert(foo.height) ; // 14
==========
See:
<https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Functions/get
>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-02-28 23:23 +0100 |
| Message-ID | <XnsA5BCEDE00D83Feejj99@194.109.6.166> |
| In reply to | #29721 |
ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in comp.lang.javascript: > "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: >>// Uncaught TypeError: Cannot set property height of #<Object> >>// which has only a getter > > Defining the setter was kindly left as an > exercise for the reader by Ram Tobolski, This being a discussiongroup, why guess at the level of kindness of a single poster?. > so you might just give it a try! Well, I just did, and being tested I found that it gives additional errors, unforseen by the unwary, because it is not an equivalent to defining an objects element, so I think it is not a nice way to implement the problem at hand. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2016-02-29 00:11 +0100 |
| Message-ID | <navur6$448$1@news.albasani.net> |
| In reply to | #29723 |
On 02/28/2016 23:23, Evertjan. wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in
> comp.lang.javascript:
>
>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
>>> // Uncaught TypeError: Cannot set property height of #<Object>
>>> // which has only a getter
>>
>> Defining the setter was kindly left as an
>> exercise for the reader by Ram Tobolski,
[...]
>> so you might just give it a try!
>
> Well, I just did, and being tested I found that it gives additional errors,
> unforseen by the unwary, because it is not an equivalent to defining an
> objects element,
I don't understand, what additional unforseen errors did you get?
Property getter/setter methods should work as advertised (where
supported) - does your object look like this?
var foo = {
"name":"bar",
"width":600,
get height () { return this.width * 2; },
set height (h) { this.width = h / 2; },
};
foo.height; // 1200
foo.width; // 600
foo.height = 1000;
foo.height; // 1000
foo.width; // 500
> I think it is not a nice way to implement the problem at hand.
I'm late to this thread, but Ram Tobolski's suggestion is spot on, if
you have property values which are simply derived from other properties
of the same object. When something more complex is needed, I'd go with a
factory function or anonymous closure.
Regarding the example you posted earlier:
>> On 02/28/2016 23:07, Evertjan. wrote:
>>> and also not so nice, imho, is:
>>>
>>> var vvv;
>>> var foo = {
>>> get height(){return vvv * 2;}
>>> };
>>> alert(foo.height) ; // NaN
>>> vvv = 20;
>>> alert(foo.height) ; // 40
>>> vvv = 7;
>>> alert(foo.height) ; // 14
Looks fine to me. It doesn't make a lot of sense in a rectangle example,
but if you let a property value depend on an unrelated variable (as
opposed to another property of the same object), that's what you get.
- stefan
[toc] | [prev] | [next] | [standalone]
| From | Aleksandro <aleksandro@gmx.com> |
|---|---|
| Date | 2016-02-29 14:49 -0300 |
| Message-ID | <nb205k$ie7$1@dont-email.me> |
| In reply to | #29723 |
On 28/02/16 19:23, Evertjan. wrote: > ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in > comp.lang.javascript: > >> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: >>> // Uncaught TypeError: Cannot set property height of #<Object> >>> // which has only a getter >> >> Defining the setter was kindly left as an >> exercise for the reader by Ram Tobolski, > > This being a discussiongroup, > why guess at the level of kindness of a single poster?. > >> so you might just give it a try! > > Well, I just did, and being tested I found that it gives additional errors, > unforseen by the unwary, because it is not an equivalent to defining an > objects element, so I think it is not a nice way to implement the problem at > hand. What errors were these? I want to see them.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-02-29 19:49 +0100 |
| Message-ID | <XnsA5BDC9B85FA3Aeejj99@194.109.6.166> |
| In reply to | #29731 |
Aleksandro <aleksandro@gmx.com> wrote on 29 Feb 2016 in comp.lang.javascript: > On 28/02/16 19:23, Evertjan. wrote: >> ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in >> comp.lang.javascript: >> >>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: >>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>> // which has only a getter >>> >>> Defining the setter was kindly left as an >>> exercise for the reader by Ram Tobolski, >> >> This being a discussiongroup, >> why guess at the level of kindness of a single poster?. >> >>> so you might just give it a try! >> >> Well, I just did, and being tested I found that it gives additional >> errors, unforseen by the unwary, because it is not an equivalent to >> defining an objects element, so I think it is not a nice way to >> implement the problem at hand. > > What errors were these? I want to see them. I showed you already, are we discussing or do you want to "win"? >>> // Uncaught TypeError: Cannot set property height of #<Object> >>> // which has only a getter This error is additional, as it does not come up with my solution. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Aleksandro <aleksandro@gmx.com> |
|---|---|
| Date | 2016-03-01 10:48 -0300 |
| Message-ID | <nb46cq$leb$1@dont-email.me> |
| In reply to | #29732 |
On 29/02/16 15:49, Evertjan. wrote: > Aleksandro <aleksandro@gmx.com> wrote on 29 Feb 2016 in > comp.lang.javascript: > >> On 28/02/16 19:23, Evertjan. wrote: >>> ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in >>> comp.lang.javascript: >>> >>>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: >>>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>>> // which has only a getter >>>> >>>> Defining the setter was kindly left as an >>>> exercise for the reader by Ram Tobolski, >>> >>> This being a discussiongroup, >>> why guess at the level of kindness of a single poster?. >>> >>>> so you might just give it a try! >>> >>> Well, I just did, and being tested I found that it gives additional >>> errors, unforseen by the unwary, because it is not an equivalent to >>> defining an objects element, so I think it is not a nice way to >>> implement the problem at hand. >> >> What errors were these? I want to see them. > > I showed you already, are we discussing or do you want to "win"? > >>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>> // which has only a getter > > This error is additional, as it does not come up with my solution. Oh, so all the madness is because you did not make a getter. /facepalm
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-03-01 17:22 +0100 |
| Message-ID | <XnsA5BEB0B2826D1eejj99@194.109.6.166> |
| In reply to | #29745 |
Aleksandro <aleksandro@gmx.com> wrote on 01 Mar 2016 in comp.lang.javascript: > On 29/02/16 15:49, Evertjan. wrote: >> Aleksandro <aleksandro@gmx.com> wrote on 29 Feb 2016 in >> comp.lang.javascript: >> >>> On 28/02/16 19:23, Evertjan. wrote: >>>> ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in >>>> comp.lang.javascript: >>>> >>>>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: >>>>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>>>> // which has only a getter >>>>> >>>>> Defining the setter was kindly left as an >>>>> exercise for the reader by Ram Tobolski, >>>> >>>> This being a discussiongroup, >>>> why guess at the level of kindness of a single poster?. >>>> >>>>> so you might just give it a try! >>>> >>>> Well, I just did, and being tested I found that it gives additional >>>> errors, unforseen by the unwary, because it is not an equivalent to >>>> defining an objects element, so I think it is not a nice way to >>>> implement the problem at hand. >>> >>> What errors were these? I want to see them. >> >> I showed you already, are we discussing or do you want to "win"? >> >>>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>>> // which has only a getter >> >> This error is additional, as it does not come up with my solution. > > Oh, so all the madness is because you did not make a getter. /facepalm No, the error is an REAL error. The OP asked how to add an object element whose value was computed from another element. Now a get DOES NOT produce such an element, but just sumulares it in some aspects, but ERRORS OUT in another aspect. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Aleksandro <aleksandro@gmx.com> |
|---|---|
| Date | 2016-03-01 15:07 -0300 |
| Message-ID | <nb4liq$kat$1@dont-email.me> |
| In reply to | #29747 |
On 01/03/16 13:22, Evertjan. wrote: > Aleksandro <aleksandro@gmx.com> wrote on 01 Mar 2016 in > comp.lang.javascript: > >> On 29/02/16 15:49, Evertjan. wrote: >>> Aleksandro <aleksandro@gmx.com> wrote on 29 Feb 2016 in >>> comp.lang.javascript: >>> >>>> On 28/02/16 19:23, Evertjan. wrote: >>>>> ram@zedat.fu-berlin.de (Stefan Ram) wrote on 28 Feb 2016 in >>>>> comp.lang.javascript: >>>>> >>>>>> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes: >>>>>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>>>>> // which has only a getter >>>>>> >>>>>> Defining the setter was kindly left as an >>>>>> exercise for the reader by Ram Tobolski, >>>>> >>>>> This being a discussiongroup, >>>>> why guess at the level of kindness of a single poster?. >>>>> >>>>>> so you might just give it a try! >>>>> >>>>> Well, I just did, and being tested I found that it gives additional >>>>> errors, unforseen by the unwary, because it is not an equivalent to >>>>> defining an objects element, so I think it is not a nice way to >>>>> implement the problem at hand. >>>> >>>> What errors were these? I want to see them. >>> >>> I showed you already, are we discussing or do you want to "win"? >>> >>>>>> // Uncaught TypeError: Cannot set property height of #<Object> >>>>>> // which has only a getter >>> >>> This error is additional, as it does not come up with my solution. >> >> Oh, so all the madness is because you did not make a getter. /facepalm > > No, the error is an REAL error. > > The OP asked how to add an object element whose value was computed from > another element. Now a get DOES NOT produce such an element, but just > sumulares it in some aspects, but ERRORS OUT in another aspect. Whenever you wrongly use a feature it might error in other aspect!!
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-03-01 20:26 +0100 |
| Message-ID | <XnsA5BECFDEEEC11eejj99@194.109.6.166> |
| In reply to | #29750 |
Aleksandro <aleksandro@gmx.com> wrote on 01 Mar 2016 in comp.lang.javascript: >> The OP asked how to add an object element whose value was computed from >> another element. Now a get DOES NOT produce such an element, but just >> sumulares it in some aspects, but ERRORS OUT in another aspect. > > Whenever you wrongly use a feature it might error in other aspect!! Whenever you answer a Q with a not fitting answer, that is what you get. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Aleksandro <aleksandro@gmx.com> |
|---|---|
| Date | 2016-03-04 21:53 -0300 |
| Message-ID | <nbdag0$ggd$1@dont-email.me> |
| In reply to | #29751 |
On 01/03/16 16:26, Evertjan. wrote: > Aleksandro <aleksandro@gmx.com> wrote on 01 Mar 2016 in comp.lang.javascript: > >>> The OP asked how to add an object element whose value was computed from >>> another element. Now a get DOES NOT produce such an element, but just >>> sumulares it in some aspects, but ERRORS OUT in another aspect. >> >> Whenever you wrongly use a feature it might error in other aspect!! > > Whenever you answer a Q with a not fitting answer, that is what you get. But it did the job. :)
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-03-05 14:22 +0100 |
| Message-ID | <XnsA5C292470C038eejj99@194.109.6.166> |
| In reply to | #29820 |
Aleksandro <aleksandro@gmx.com> wrote on 05 Mar 2016 in comp.lang.javascript: > On 01/03/16 16:26, Evertjan. wrote: >> Aleksandro <aleksandro@gmx.com> wrote on 01 Mar 2016 in >> comp.lang.javascript: >> >>>> The OP asked how to add an object element whose value was computed >>>> from another element. Now a get DOES NOT produce such an element, but >>>> just sumulares it in some aspects, but ERRORS OUT in another aspect. >>> >>> Whenever you wrongly use a feature it might error in other aspect!! >> >> Whenever you answer a Q with a not fitting answer, that is what you >> get. > > But it did the job. :) That might be allright in a personal conversation, though I even doubt that. The answer was inexact and so unfit for this NG, where many others could be set on the wrong foot. In short, the result never not justify the means. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2016-03-05 10:17 -0800 |
| Message-ID | <00d49959-20f9-43ec-8d80-dc2d3991a31c@googlegroups.com> |
| In reply to | #29823 |
Evertjan. wrote: > Aleksandro wrote: >> But it did the job. :) > [ ... ] > The answer was inexact and so unfit for this NG, where many others > could be set on the wrong foot. > [ ... ] Evertjan, I don't know where this is coming from. As Stefan pointed out, the answer from Ram Tobolski was the best one supplied, better than mine, better than the ones from Christoph Becker and Thomas Lahn. All of us had solutions that would do the job, but Ram's answer did the job and was the most simple. Thomas Lahn, was unsurprisingly the most comprehensive in suggesting possible approaches, but even his simplest one was more complex than Ram's suggestion. The OP did not specifically ask for a solution with a configurable `height` property, but it's a very, very obvious extension to Ram's answer. If the OP wasn't sure and didn't test, an quick question would have confirmed that it would work as expected. So, can you see anything wrong with this suggestion, or with the slight extension proposed by Stefan? -- Scott
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-03-06 11:59 +0100 |
| Message-ID | <XnsA5C379E8FC4CDeejj99@194.109.6.166> |
| In reply to | #29825 |
Scott Sauyet <scott.sauyet@gmail.com> wrote on 05 Mar 2016 in
comp.lang.javascript:
> Evertjan. wrote:
>> Aleksandro wrote:
>
>>> But it did the job. :)
>> [ ... ]
>> The answer was inexact and so unfit for this NG, where many others
>> could be set on the wrong foot.
>> [ ... ]
>
> Evertjan,
>
> I don't know where this is coming from.
Silly, it come from me, as you clearly quote!
> As Stefan pointed out, the
> answer from Ram Tobolski was the best one supplied,
An absolute "the best one"???????????? [see below]
> better than mine,
> better than the ones from Christoph Becker and Thomas Lahn. All of us
> had solutions that would do the job, but Ram's answer did the job and
> was the most simple. Thomas Lahn, was unsurprisingly the most
> comprehensive in suggesting possible approaches, but even his simplest
> one was more complex than Ram's suggestion.
>
> The OP did not specifically ask for a solution with a configurable
> `height` property, but it's a very, very obvious extension to Ram's
> answer. If the OP wasn't sure and didn't test, an quick question
> would have confirmed that it would work as expected.
>
> So, can you see anything wrong with this suggestion, or with the
> slight extension proposed by Stefan?
I agree, it was the best iyho, but not in imho.
var foo { "name":"bar", "width":600 };
foo.height = foo.width * 2;
... makes results in what is real usefull,
as it does not suffer from the error possibillity,
if someone later wants to change foo.height.
if foo.height is just a 'getter', a later:
foo.height = foo.width * 3;
using 'getter' would throw unexpectedly:
>// Uncaught TypeError: Cannot set property height of #<Object>
>// which has only a getter
which imho does not getter better.
The 'getter' method is nice and usefull,
but less simple and more error-prone,
as most shortcut codes are.
Mixing 'getters' with ordinary object properties,
would perhaps need a naming-convention that alerts the unwary,
like:
var foo = {
'nameProperty':'bar',
'widthProperty':600,
get heightGetter(){return this.width * 2;}
};
Thomas c/should come up with a better naming-convention.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web