aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/object/index.html
blob: cf55c9b004cd1ebc538346f55e5e44c3f7e5bb88 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
title: Object
slug: Web/JavaScript/Reference/Global_Objects/Object
tags:
  - Constructor
  - JavaScript
  - NeedsTranslation
  - Object
  - TopicStub
translation_of: Web/JavaScript/Reference/Global_Objects/Object
---
<div>{{JSRef}}</div>

<p><code><strong>Object</strong></code> 建構式可用於建立物件包裝(object wrapper)。</p>

<h2 id="語法">語法</h2>

<pre class="syntaxbox">// Object initialiser or literal
{ [ <var>nameValuePair1</var>[, <var>nameValuePair2</var>[, ...<var>nameValuePairN</var>] ] ] }

// Called as a constructor
new Object([<var>value</var>])</pre>

<h3 id="參數">參數</h3>

<dl>
 <dt><code>nameValuePair1, nameValuePair2, ... nameValuePair<em>N</em></code></dt>
 <dd>Pairs of names (strings) and values (any value) where the name is separated from the value by a colon.</dd>
 <dt><code>value</code></dt>
 <dd>任意值。</dd>
</dl>

<h2 id="描述">描述</h2>

<p>The <code>Object</code> constructor creates an object wrapper for the given value. If the value is {{jsxref("null")}} or {{jsxref("undefined")}}, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value.</p>

<p>When called in a non-constructor context, <code>Object</code> behaves identically to <code>new Object()</code>.</p>

<p>也可以參考 <a href="/zh-TW/docs/Web/JavaScript/Reference/Operators/Object_initializer">object initializer / literal syntax</a>.</p>

<h2 id="Object_建構式屬性"><code>Object</code> 建構式屬性</h2>

<dl>
 <dt><code>Object.length</code></dt>
 <dd>Has a value of 1.</dd>
 <dt>{{jsxref("Object.prototype")}}</dt>
 <dd>Allows the addition of properties to all objects of type Object.</dd>
</dl>

<h2 id="Object_建構式方法"><code>Object</code> 建構式方法</h2>

<dl>
 <dt>{{jsxref("Object.assign()")}}</dt>
 <dd>Creates a new object by copying the values of all enumerable own properties from one or more source objects to a target object.</dd>
 <dt>{{jsxref("Object.create()")}}</dt>
 <dd>Creates a new object with the specified prototype object and properties.</dd>
 <dt>{{jsxref("Object.defineProperty()")}}</dt>
 <dd>Adds the named property described by a given descriptor to an object.</dd>
 <dt>{{jsxref("Object.defineProperties()")}}</dt>
 <dd>Adds the named properties described by the given descriptors to an object.</dd>
 <dt>{{jsxref("Object.entries()")}} {{experimental_inline}}</dt>
 <dd>Returns an array of a given object's own enumerable property <code>[key, value]</code> pairs.</dd>
 <dt>{{jsxref("Object.freeze()")}}</dt>
 <dd>Freezes an object: other code can't delete or change any properties.</dd>
 <dt>{{jsxref("Object.getOwnPropertyDescriptor()")}}</dt>
 <dd>Returns a property descriptor for a named property on an object.</dd>
 <dt>{{jsxref("Object.getOwnPropertyDescriptors()")}}</dt>
 <dd>Returns an object containing all own property descriptors for an object.</dd>
 <dt>{{jsxref("Object.getOwnPropertyNames()")}}</dt>
 <dd>Returns an array containing the names of all of the given object's <strong>own</strong> enumerable and non-enumerable properties.</dd>
 <dt>{{jsxref("Object.getOwnPropertySymbols()")}}</dt>
 <dd>Returns an array of all symbol properties found directly upon a given object.</dd>
 <dt>{{jsxref("Object.getPrototypeOf()")}}</dt>
 <dd>Returns the prototype of the specified object.</dd>
 <dt>{{jsxref("Object.is()")}}</dt>
 <dd>Compares if two values are distinguishable (ie. the same)</dd>
 <dt>{{jsxref("Object.isExtensible()")}}</dt>
 <dd>Determines if extending of an object is allowed.</dd>
 <dt>{{jsxref("Object.isFrozen()")}}</dt>
 <dd>Determines if an object was frozen.</dd>
 <dt>{{jsxref("Object.isSealed()")}}</dt>
 <dd>Determines if an object is sealed.</dd>
 <dt>{{jsxref("Object.keys()")}}</dt>
 <dd>Returns an array containing the names of all of the given object's <strong>own</strong> enumerable properties.</dd>
 <dt>{{jsxref("Object.preventExtensions()")}}</dt>
 <dd>Prevents any extensions of an object.</dd>
 <dt>{{jsxref("Object.seal()")}}</dt>
 <dd>Prevents other code from deleting properties of an object.</dd>
 <dt>{{jsxref("Object.setPrototypeOf()")}}</dt>
 <dd>Sets the prototype (i.e., the internal <code>[[Prototype]]</code> property)</dd>
 <dt>{{jsxref("Object.values()")}} {{experimental_inline}}</dt>
 <dd>Returns an array of a given object's own enumerable values.</dd>
</dl>

<h2 id="Object_物件實體與_Object_原型物件"><code>Object</code> 物件實體與 <code>Object</code> 原型物件</h2>

<p>All objects in JavaScript are descended from <code>Object</code>; all objects inherit methods and properties from {{jsxref("Object.prototype")}}, although they may be overridden. For example, other constructors' prototypes override the <code>constructor</code> property and provide their own <code>toString()</code> methods. Changes to the <code>Object</code> prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.</p>

<h3 id="屬性">屬性</h3>

<div>{{page('/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype', '屬性') }}</div>

<h3 id="方法">方法</h3>

<div>{{page('/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype', '方法') }}</div>

<h2 id="範例">範例</h2>

<h3 id="Using_Object_given_undefined_and_null_types">Using <code>Object</code> given <code>undefined</code> and <code>null</code> types</h3>

<p>下面例子儲存一個空物件至變數o</p>

<pre class="brush: js">var o = new Object();
</pre>

<pre class="brush: js">var o = new Object(undefined);
</pre>

<pre class="brush: js">var o = new Object(null);
</pre>

<h3 id="Using_Object_to_create_Boolean_objects">Using <code>Object</code> to create <code>Boolean</code> objects</h3>

<p>下面例子儲存 {{jsxref("Boolean")}} 物件在 <code>o</code>:</p>

<pre class="brush: js">// equivalent to o = new Boolean(true);
var o = new Object(true);
</pre>

<pre class="brush: js">// equivalent to o = new Boolean(false);
var o = new Object(Boolean());
</pre>

<h2 id="規範">規範</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>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.2', 'Object')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-object-objects', 'Object')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Added Object.assign, Object.getOwnPropertySymbols, Object.setPrototypeOf, Object.is</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object-objects', 'Object')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>Added Object.entries, Object.values and Object.getOwnPropertyDescriptors.</td>
  </tr>
 </tbody>
</table>

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="參見">參見</h2>

<ul>
 <li><a href="/zh-TW/docs/Web/JavaScript/Reference/Operators/Object_initializer">Object initializer</a></li>
</ul>