Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #25954 > unrolled thread

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

Started byUne Bévue <unbewusst.sein@fai.invalid>
First post2014-10-21 12:06 +0200
Last post2014-10-22 10:22 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  when have we to use obj.hasOwnProperty(key) ? Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 12:06 +0200
    Re: when have we to use obj.hasOwnProperty(key) ? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2014-10-21 13:21 -0200
      Re: when have we to use obj.hasOwnProperty(key) ? Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-21 17:26 +0200
    Re: when have we to use obj.hasOwnProperty(key) ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-10-21 19:27 +0200
      Re: when have we to use obj.hasOwnProperty(key) ? Une Bévue <unbewusst.sein@fai.invalid> - 2014-10-22 10:22 +0200

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

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2014-10-21 12:06 +0200
Subjectwhen have we to use obj.hasOwnProperty(key) ?
Message-ID<m25b7r$554$1@shakotay.alphanet.ch>
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 ?

[toc] | [next] | [standalone]


#25963

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2014-10-21 13:21 -0200
Message-ID<m25tmj$tts$1@speranza.aioe.org>
In reply to#25954
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-table/es5/>

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

[toc] | [prev] | [next] | [standalone]


#25964

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2014-10-21 17:26 +0200
Message-ID<m25tvn$mkv$1@shakotay.alphanet.ch>
In reply to#25963
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-table/es5/>
>
> 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 !

[toc] | [prev] | [next] | [standalone]


#25968

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-10-21 19:27 +0200
Message-ID<2573276.tGzZJPnWE1@PointedEars.de>
In reply to#25954
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.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]


#25972

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2014-10-22 10:22 +0200
Message-ID<m27pg8$7jv$2@shakotay.alphanet.ch>
In reply to#25968
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web