aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/reference/global_objects/object/hasownproperty/index.html
blob: 3a05d6d5aeef41455249bcb54521f4f9c97ca76c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
title: Object.prototype.hasOwnProperty()
slug: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
translation_of: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
original_slug: Web/JavaScript/Referencje/Obiekty/Object/hasOwnProperty
---
<div>{{JSRef("Global_Objects", "Object")}}</div>

<h2 id="Summary" name="Summary">Wstęp</h2>

<p>Metoda <code><strong>hasOwnProperty()</strong></code> zwraca wartość <code>true</code> jeśli obiekt, na którym została wywołana posiada konkretną własność.</p>

<h2 id="Syntax" name="Syntax">Składnia</h2>

<pre class="syntaxbox notranslate"><code><var>obj</var>.hasOwnProperty(<var>prop</var>)</code></pre>

<h3 id="Parameters" name="Parameters">Parametry</h3>

<dl>
 <dt><code>prop</code></dt>
 <dd>Nazwa ({{jsxref("String")}}) własności lub <a href="https://developer.mozilla.org/en-US/docs/Glossary/Symbol">Symbol</a>, do sprawdzenia.</dd>
</dl>

<h3 id="Wartość_zwracana">Wartość zwracana</h3>

<p><span class="tlid-translation translation"><span class="alt-edited">A {{jsxref ("Boolean")}} wskazujący, czy obiekt zawiera w sobie określoną właściwość.</span></span></p>

<h2 id="Description" name="Description">Opis</h2>

<p>Każdy obiekt pochodzący od {{jsxref("Global_Objects/Object", "Object")}} dziedziczy metodę <code>hasOwnProperty</code>. Może być ona użyta do stwierdzenia czy obiekt posiada określoną własność, jako bezpośrednią (direct) własność. W przeciwieństwie do  operatora {{jsxref("Operators/in", "in")}}, metoda <code style="font-style: normal;">hasOwnProperty</code> nie sprawdza <u>w głąb łańcucha</u> własności obiektu.</p>

<h2 id="Examples" name="Examples">Notatka</h2>

<p><span class="tlid-translation translation"><span title=""><code>hasOwnProperty</code> zwraca <code>true</code>, nawet jeśli wartość właściwości to <code>null</code> lub <code>undefined</code>.</span></span></p>

<pre class="brush: js line-numbers  language-js notranslate"><code class="language-js">o <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Object</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
o<span class="punctuation token">.</span>propOne <span class="operator token">=</span> <span class="keyword token">null</span><span class="punctuation token">;</span>
o<span class="punctuation token">.</span><span class="function token">hasOwnProperty</span><span class="punctuation token">(</span><span class="string token">'propOne'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>   <span class="comment token">// zwraca true</span>
o<span class="punctuation token">.</span>propTwo <span class="operator token">=</span> undefined<span class="punctuation token">;</span>
o<span class="punctuation token">.</span><span class="function token">hasOwnProperty</span><span class="punctuation token">(</span><span class="string token">'propTwo'</span><span class="punctuation token">)</span><span class="punctuation token">;   // zwraca true</span></code>
</pre>

<h2 id="Examples" name="Examples">Przykłady</h2>

<h3 id="Example_Using_hasOwnProperty_to_test_for_a_property.27s_existence" name="Example:_Using_hasOwnProperty_to_test_for_a_property.27s_existence">Użycie <code>hasOwnProperty</code> do testowania istnienia własności</h3>

<p>Poniższy przykład określa czy obiekt <code>o</code> posiada własność o nazwie <code>prop</code>:</p>

<pre class="brush: js line-numbers  language-js notranslate"><code class="language-js">o <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Object</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
o<span class="punctuation token">.</span><span class="function token">hasOwnProperty</span><span class="punctuation token">(</span><span class="string token">'prop'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>   <span class="comment token">// zwraca false</span>
o<span class="punctuation token">.</span>prop <span class="operator token">=</span> <span class="string token">'istnieje'</span><span class="punctuation token">;</span>
o<span class="punctuation token">.</span><span class="function token">hasOwnProperty</span><span class="punctuation token">(</span><span class="string token">'prop'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>   <span class="comment token">// zwraca true</span></code></pre>

<h3 id="Example_Direct_versus_inherited_properties" name="Example:_Direct_versus_inherited_properties">Własności bezpośrednie kontra odziedziczone</h3>

<p>Poniższy przykład rozróżnia bezpośrednie właściwości z właściwościami dziedziczonymi w łańcuchu prototypów:</p>

<pre class="brush: js notranslate">o = new Object();
o.prop = 'istnieje';
o.hasOwnProperty('prop');             // zwraca true
o.hasOwnProperty('toString');         // zwraca false
o.hasOwnProperty('hasOwnProperty');   // zwraca false
</pre>

<h3 id="Example_Itarate_over_properties_not_considering_inherited_properties" name="Example:_Itarate_over_properties_not_considering_inherited_properties">Iterowanie przez właściwości obiektu</h3>

<p><span class="tlid-translation translation"><span title="">Poniższy przykład ilustruje sposób iteracji po właściwościach obiektu bez wykonywania iteracji na dziedziczonych właściwościach.</span></span></p>

<pre class="notranslate">const obj = {
  prop: 'Wartość',
  secondProp: 'Wartość 2'
}

for (const name in obj) {
  if (obj.hasOwnProperty(name)) {
    console.log('Znaleziono własność ' + name + ' o wartości ' + obj[name])
  } else {
    console.log('Brak własności: ', name)
  }
}</pre>

<div class="blockIndicator warning">
<p><span class="tlid-translation translation"><span title="">Zwróć uwagę, że pętla </span></span>{{jsxref("Statements/for...in", "for...in")}}<span class="tlid-translation translation"><span title=""> tylko iteruje właściwości przeliczalne</span></span>, a <a href="http://zduck.com/2013/non-enumerable-properties-in-javascript/">nieprzeliczanych</a> nie zauważa.</p>
</div>

<h3 id="Używanie_hasOwnProperty_jako_nazwy_właściwości"><span class="tlid-translation translation"><span title="">Używanie <code>hasOwnProperty</code> jako nazwy właściwości</span></span></h3>

<p><span class="tlid-translation translation"><span title="">JavaScript nie chroni nazwy właściwości <code>hasOwnProperty</code>;</span> <span title="">tak więc, jeśli istnieje możliwość, że obiekt może mieć właściwość o tej nazwie, konieczne jest użycie zewnętrznej właściwości <code>hasOwnProperty</code>, aby uzyskać poprawne wyniki:</span></span></p>

<pre class="brush: js notranslate">const obj = {
  hasOwnProperty: function() {
    return false;
  },
  prop: 'Kolejna właściwość'
};

obj.hasOwnProperty('prop'); // zawsze zwróci false

// <span class="tlid-translation translation"><span class="alt-edited">Użyj właściwości hasOwnProperty innego obiektu i wywołaj ją, ustawiając "this" na obj</span></span>
({}).hasOwnProperty.call(obj, 'prop'); // zwraca true

// <span class="tlid-translation translation"><span title="">W tym celu można również użyć własności hasOwnProperty z prototypu Object</span></span>
Object.prototype.hasOwnProperty.call(obj, 'prop'); // zwraca true
</pre>

<p><span class="tlid-translation translation"><span title="">Zwróć uwagę, że w ostatnim przypadku nie ma żadnych nowo utworzonych obiektów.</span></span></p>

<h2 id="Specyfikacja">Specyfikacja</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition.</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.5.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2.4.5', 'Object.prototype.hasOwnProperty')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object.prototype.hasownproperty', 'Object.prototype.hasOwnProperty')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Wsparcie_przeglądarek">Wsparcie przeglądarek</h2>

<div class="hidden">
<p><span class="tlid-translation translation"><span title="">Tabela zgodności na tej stronie jest generowana na podstawie danych strukturalnych.</span> <span title="">Jeśli chcesz przyczynić się do danych, sprawdź <a href="/pl/docs/">https://github.com/mdn/browser-compat-data</a> i wyślij nam pull request'a.</span></span></p>
</div>

<div>{{Compat("javascript.builtins.Object.hasOwnProperty")}}</div>

<h2 id="See_also" name="See_also">Zobacz także</h2>

<ul>
 <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
 <li>{{jsxref("Object.getOwnPropertyNames()")}}</li>
 <li>{{jsxref("Statements/for...in", "for...in")}}</li>
 <li>{{jsxref("Operators/in", "in")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Inheritance_Revisited">JavaScript Guide: Inheritance revisited</a></li>
</ul>