From: kangax on
Asen Bozhilov wrote:
> On 1 Ноем, 07:31, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
>> AIUI, it means the object has no own properties.
>>
>> The example has the footnote:
>> | The aim of this function is to detect properties directly on obj.
>>
>> It fails if the program modifies Object.prototype. That can and should
>> avoided.
[snip sig]
>
> Of course. You have in APE one method `mixin' who copy own properties.
> Here we can use similar approach.
>
> Object.hasOwnProperties = function(o)
> {
> for (var i in o)
> {
> if (o.hasOwnProperty(i)) return true;

It would be safer to replace this with:

if (Object.prototype.hasOwnProperty.call(o, i)) { /* ... */ }

(probably also aliasing `Object.prototype.hasOwnProperty` for faster access)

> }
> return false;
> };
>
> Except {DontEnum} bug of JScript for properties `valueOf' and
> `toString'.

.... and any other built-in `Object.prototype` property.

--
kangax
From: Asen Bozhilov on
On 1 Ноем, 17:32, kangax <kan...(a)gmail.com> wrote:

> It would be safer to replace this with:
>
> if (Object.prototype.hasOwnProperty.call(o, i)) { /* ... */ }
>
> (probably also aliasing `Object.prototype.hasOwnProperty` for faster access)

When i write that code, i think about your suggestion. I don't think
Object.prototype.hasOwnProperty.call(o, i) is safer. I don't know what
is behavior of hasOwnProperty over a host object and i want to test
`object' who inherit `hasOwnProperty' from Object.prototype or
implement *own* `hasOwnProperty'. See below:

var a = '{DontDelete}';
Object.prototype.hasOwnProperty.call(this, 'a'); //true

Global execution context doesn't have Variable Object, for Variable
Object used Global Object. Every variable will be property of Global
Object but unlike other user defined properties of objects, they have
attribute {DontDelete}.

Another problem in global execution context is IE {DontEnum} bug:

p = 'IE {DontEnum}';
for (var i in this)
{
if (i == 'p') window.alert(this[i]);
}
window.alert('p' in this); //true





From: kangax on
Asen Bozhilov wrote:
> On 1 Ноем, 17:32, kangax <kan...(a)gmail.com> wrote:
>
>> It would be safer to replace this with:
>>
>> if (Object.prototype.hasOwnProperty.call(o, i)) { /* ... */ }
>>
>> (probably also aliasing `Object.prototype.hasOwnProperty` for faster access)
>
> When i write that code, i think about your suggestion. I don't think
> Object.prototype.hasOwnProperty.call(o, i) is safer. I don't know what
> is behavior of hasOwnProperty over a host object and i want to test
> `object' who inherit `hasOwnProperty' from Object.prototype or
> implement *own* `hasOwnProperty'. See below:

But how do you know that an object (and I'm not talking about host one)
has `hasOwnProperty` that is callable or even that it references correct
(`Object.prototype.hasOwnProperty`) function?

var o = { 'hasOwnProperty': 'foo' };
o.hasOwnProperty('bar'); // TypeError

You can try testing for it, but why bother.
`Object.prototype.hasOwnProperty.call` doesn't have these problems.

>
> var a = '{DontDelete}';
> Object.prototype.hasOwnProperty.call(this, 'a'); //true
>
> Global execution context doesn't have Variable Object, for Variable

Well actually it does. When in global code, Global Object acts as a
Variable Object.

> Object used Global Object. Every variable will be property of Global
> Object but unlike other user defined properties of objects, they have
> attribute {DontDelete}.

Yep, properties of Global Object don't have {DontDelete} set. Variables do.

>
> Another problem in global execution context is IE {DontEnum} bug:
>
> p = 'IE {DontEnum}';

Undeclared assignment. Results in a property creation...

> for (var i in this)
> {
> if (i == 'p') window.alert(this[i]);
> }

.... that has {DontEnum} set in IE. See table in
<http://groups.google.com/group/comp.lang.javascript/msg/dda4dee3390fa71a>

[...]

--
kangax
From: Asen Bozhilov on
On 1 Ноем, 21:30, kangax <kan...(a)gmail.com> wrote:

> var o = { 'hasOwnProperty': 'foo' };
> o.hasOwnProperty('bar'); // TypeError
>
> You can try testing for it, but why bother.
> `Object.prototype.hasOwnProperty.call` doesn't have these problems.

Yes, but this can interpreted in different way. With
Object.prototype.hasOwnProperty.call you break implementation of *own*
`hasOwnProperty' of `object'. All of that is point of view. And for me
inherited `hasOwnProperty' or *own* is better from
Object.prototype.hasOwnProperty.call .

> ... that has {DontEnum} set in IE. See table in
> <http://groups.google.com/group/comp.lang.javascript/msg/dda4dee3390fa71a>

Thankс for link, usefully topic. I read it before when it was
released