Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17416
| Message-ID | <2075691.byxZkP05Om@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-12-02 19:35 +0100 |
| Subject | Re: Problem w/ creating dynamic TR and its TDs |
| Newsgroups | comp.lang.javascript |
| References | (1 earlier) <k9e45v$d6v$1@speranza.aioe.org> <5462881.fXVs31Ce0e@PointedEars.de> <k9f0rg$ajv$1@speranza.aioe.org> <5551855.fYihAxhPQP@PointedEars.de> <k9g4a1$54q$1@speranza.aioe.org> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Cezary Tomczyk wrote:
> W dniu 2012-12-02 16:11, Thomas 'PointedEars' Lahn pisze:
>>> Cezary Tomczyk wrote:
> [...]
> Personally I do not care about IE7 and lower. Currently I am focusing on
> IE8 and higher.
Given the market share of the versions you do not care about, you should
reconsider, as bad as that sounds to both of us.
> However, thanks for explanation.
You're welcome.
>>> See test: http://jsperf.com/loop-for-in-vs-object-keys-foreach/2
>>>
>>> And yes, I remember your opinion about those tests :-)
>>
>> Good. You should also know that you are comparing apples and oranges
>> here.
>
> Maybe. It is just a overall test.
Which is pointless if you do not consider that not only the functionality of
the tested features is fundamentally different but also that you are working
against the advantages of certain variants. Why forEach()? Why i++ where
you count with j? What about ordering?
>> A for-in statement is _not_ equivalent to either foreach (or the E4X `for
>> each') or Object.keys(). By contrast, it iterates over own and
>> *inherited* *enumerable* properties in *implementation-dependent* (in
>> ES3, arbitrary) order; the iterator is the property name, not the
>> property value.
>>
>> Object.keys() returns a reference to an Array of the names of all *own*
>> enumerable properties (in the same implementation-dependent order as for
>> for-in), and *only* those. [Which is why if you want to emulate
>> Object.keys() for enumerable properties, you have to use
>> Object.prototype.hasOwnProperty() or emulations thereof.]
>
> I agree with you. Just forgot to get only direct properties of object. I
> use:
>
> " [...]
> for (key in params) {
> if (Object.prototype.hasOwnProperty.call(params, key)) {
> [...]
> "
Unless you cannot be certain that the value of `params' refers to an object
that has Object.prototype in its prototype chain, this is needlessly
inefficient. Keep in mind that identifier lookup is a runtime feature. In
each iteration `Object' will have to be resolved against the scope chain
(usually up to the global object), then its `prototype' property will have
to be resolved against the prototype chain, and so forth.
Consider this instead:
for (var key in params)
{
if (params.hasOwnProperty(key))
{
// …
}
}
The least you should do is to cache the value of
`Object.prototype.hasOwnProperty', maybe even in a bound variable of a
closure (as can be seen in JSX), if you do not call it through the prototype
chain.
/*
* Or `Object.prototype', but `{}' appears to have a shorter
* scope chain, which might win over the longer prototype chain
*/
var _hasOwnProperty = {}.hasOwnProperty;
for (var key in params)
{
if (_hasOwnProperty.call(params, key))
{
// …
}
}
It might be useful to combine these:
var hasHasOwnProperty =
(typeof params.hasOwnProperty == "function")
? params.hasOwnProperty
: {}.hasOwnProperty;
for (var key in params)
{
if (_hasOwnProperty.call(params, key))
{
// …
}
}
That would allow the object to inherit or define its special
hasOwnProperty() method, and use the built-in as fallback.
> [...]
>> Please trim your quotes.
>
> Should be better now.
Yes, thanks.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Problem w/ creating dynamic TR and its TDs justaguy <lichunshen84@gmail.com> - 2012-12-01 15:17 -0800
Re: Problem w/ creating dynamic TR and its TDs Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-12-02 00:34 +0100
Re: Problem w/ creating dynamic TR and its TDs justaguy <lichunshen84@gmail.com> - 2012-12-01 17:06 -0800
Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 03:46 +0100
Re: Problem w/ creating dynamic TR and its TDs justaguy <lichunshen84@gmail.com> - 2012-12-01 19:27 -0800
Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-03 22:53 +0100
Re: Problem w/ creating dynamic TR and its TDs Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-12-02 08:43 +0100
Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 16:11 +0100
Re: Problem w/ creating dynamic TR and its TDs Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2012-12-02 18:48 +0100
Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 19:35 +0100
Re: Problem w/ creating dynamic TR and its TDs Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-12-02 19:39 +0100
csiph-web