[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

when have we to use obj.hasOwnProperty(key) ?

Une Bévue

10/21/2014 10:07:00 AM

I'm using something like :

var obj = {};
obj.foo = 'bar';

If I look at this object in the wrowser inspector I see :

Object
foo: 'bar'
__PROTO__ : Object

the later with a lot of methods.

For the time being I never have a prob with unwanted properties, but I
wonder if...

Because this object is send to a websocket server by Json and then
included in MongoDB database.

Then does I have to filter out this object or not ?
4 Answers

JR

10/21/2014 3:22:00 PM

0

On 21/10/2014 08:06, Une Bévue wrote:
> I'm using something like :
>
> var obj = {};
> obj.foo = 'bar';
>
> If I look at this object in the wrowser inspector I see :
>
> Object
> foo: 'bar'
> __PROTO__ : Object
>
> the later with a lot of methods.
>
> For the time being I never have a prob with unwanted properties, but I
> wonder if...
>
> Because this object is send to a websocket server by Json and then
> included in MongoDB database.
>
> Then does I have to filter out this object or not ?

You can use Object.create(null) to create an Object with null as
prototype. This method is supported in IE9+ and the other major
browsers. See the ES5 compatibility table by Kangax:
<http://kangax.github.io/compat-tabl...

var obj = Object.create(null);
obj.foo = 'bar';
console.log(obj);

On the other hand, var obj = {} is equivalent to:
var obj = Object.create(Object.prototype);

--
Joao Rodrigues

Une Bévue

10/21/2014 3:27:00 PM

0

Le 21/10/14 17:21, Joao Rodrigues a écrit :
> You can use Object.create(null) to create an Object with null as
> prototype. This method is supported in IE9+ and the other major
> browsers. See the ES5 compatibility table by Kangax:
> <http://kangax.github.io/compat-tabl...
>
> var obj = Object.create(null);
> obj.foo = 'bar';
> console.log(obj);
>
> On the other hand, var obj = {} is equivalent to:
> var obj = Object.create(Object.prototype);

Fine, cristal clear, thanks !

Thomas 'PointedEars' Lahn

10/21/2014 5:28:00 PM

0

Une Bévue wrote:

> I'm using something like :
>
> var obj = {};
> obj.foo = 'bar';
>
> If I look at this object in the wrowser inspector I see :
>
> Object
> foo: 'bar'
> __PROTO__ : Object
>
> the later with a lot of methods.

_latter_

> For the time being I never have a prob with unwanted properties, but I
> wonder if...
>
> Because this object is send to a websocket server by Json and then
> included in MongoDB database.
>
> Then does I have to filter out this object or not ?

It is unclear what you are asking. However, if you want a reference to an
object that does not inherit any properties, use

var obj = Object.create(null);

If this method is implemented as specified, you do not have to use
obj.hasOwnProperty() when iterating over the object's properties with a for-
in statement.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Une Bévue

10/22/2014 8:23:00 AM

0

Le 21/10/14 19:27, Thomas 'PointedEars' Lahn a écrit :
> It is unclear what you are asking. However, if you want a reference to an
> object that does not inherit any properties, use
>
> var obj = Object.create(null);
>
> If this method is implemented as specified, you do not have to use
> obj.hasOwnProperty() when iterating over the object's properties with a for-
> in statement.

Fine, your answer is clearly what I was asking for.