aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html169
1 files changed, 169 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html
new file mode 100644
index 0000000000..996a67689e
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/object/getownpropertynames/index.html
@@ -0,0 +1,169 @@
+---
+title: Object.getOwnPropertyNames()
+slug: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
+tags:
+ - ECMAScript 5
+ - JavaScript
+ - JavaScript 1.8.5
+ - Method
+ - Object
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>Object.getOwnPropertyNames()</code></strong>方法返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组。</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox">Object.getOwnPropertyNames(<em>obj</em>)</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><code>obj</code></dt>
+ <dd>一个对象,其自身的可枚举和不可枚举属性的名称被返回。</dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>在给定对象上找到的自身属性对应的字符串数组。</p>
+
+<h2 id="Description" name="Description" style="margin-bottom: 20px; line-height: 30px;">描述</h2>
+
+<p><code>Object.getOwnPropertyNames()</code> 返回一个数组,该数组对元素是 <code>obj</code>自身拥有的枚举或不可枚举属性名称字符串。 数组中枚举属性的顺序与通过 {{jsxref("Statements/for...in", "for...in")}} 循环(或 {{jsxref("Object.keys")}})迭代该对象属性时一致。数组中不可枚举属性的顺序未定义。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="使用_Object.getOwnPropertyNames()">使用 <code>Object.getOwnPropertyNames()</code></h3>
+
+<pre class="brush: js">var arr = ["a", "b", "c"];
+console.log(Object.getOwnPropertyNames(arr).sort()); // ["0", "1", "2", "length"]
+
+// 类数组对象
+var obj = { 0: "a", 1: "b", 2: "c"};
+console.log(Object.getOwnPropertyNames(obj).sort()); // ["0", "1", "2"]
+
+// 使用Array.forEach输出属性名和属性值
+Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
+ console.log(val + " -&gt; " + obj[val]);
+});
+// 输出
+// 0 -&gt; a
+// 1 -&gt; b
+// 2 -&gt; c
+
+//不可枚举属性
+var my_obj = Object.create({}, {
+ getFoo: {
+ value: function() { return this.foo; },
+ enumerable: false
+ }
+});
+my_obj.foo = 1;
+
+console.log(Object.getOwnPropertyNames(my_obj).sort()); // ["foo", "getFoo"]
+</pre>
+
+<p>如果你只要获取到可枚举属性,查看{{jsxref("Object.keys")}}或用{{jsxref("Statements/for...in", "for...in")}}循环(还会获取到原型链上的可枚举属性,不过可以使用{{jsxref("Object.prototype.hasOwnProperty()", "hasOwnProperty()")}}方法过滤掉)。</p>
+
+<p>下面的例子演示了该方法不会获取到原型链上的属性:</p>
+
+<pre class="brush: js">function ParentClass() {}
+ParentClass.prototype.inheritedMethod = function() {};
+
+function ChildClass() {
+  this.prop = 5;
+  this.method = function() {};
+}
+
+ChildClass.prototype = new ParentClass;
+ChildClass.prototype.prototypeMethod = function() {};
+
+console.log(
+  Object.getOwnPropertyNames(
+  new ChildClass() // ["prop", "method"]
+  )
+);
+</pre>
+
+<h3 id="只获取不可枚举的属性">只获取不可枚举的属性</h3>
+
+<p>下面的例子使用了 {{jsxref("Array.prototype.filter()")}} 方法,从所有的属性名数组(使用<code>Object.getOwnPropertyNames()</code>方法获得)中去除可枚举的属性(使用{{jsxref("Object.keys()")}}方法获得),剩余的属性便是不可枚举的属性了:</p>
+
+<pre class="brush: js">var target = myObject;
+var enum_and_nonenum = Object.getOwnPropertyNames(target);
+var enum_only = Object.keys(target);
+var nonenum_only = enum_and_nonenum.filter(function(key) {
+    var indexInEnum = enum_only.indexOf(key);
+    if (indexInEnum == -1) {
+        // 没有发现在enum_only健集中意味着这个健是不可枚举的,
+        // 因此返回true 以便让它保持在过滤结果中
+        return true;
+    } else {
+        return false;
+    }
+});
+
+console.log(nonenum_only);</pre>
+
+<pre>注:<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter">Array.filter(filt_func)方法</a>创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。</pre>
+
+<h2 id="Notes" name="Notes">提示</h2>
+
+<p>在 ES5 中,如果参数不是一个原始对象类型,将抛出一个 {{jsxref("TypeError")}}  异常。在 ES2015 中,非对象参数被强制转换为对象 <strong>。</strong></p>
+
+<pre class="brush: js">Object.getOwnPropertyNames('foo');
+// TypeError: "foo" is not an object (ES5 code)
+
+Object.getOwnPropertyNames('foo');
+// ['length', '0', '1', '2'] (ES2015 code)
+</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('ES5.1', '#sec-15.2.3.4', 'Object.getOwnPropertyNames')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Initial definition. Implemented in JavaScript 1.8.5.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-object.getownpropertynames', 'Object.getOwnPropertyNames')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容">浏览器兼容</h2>
+
+<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
+
+<p>{{Compat("javascript.builtins.Object.getOwnPropertyNames")}}</p>
+
+<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>
+
+<p>Firefox 28 {{geckoRelease("28")}}之前,<code>Object.getOwnPropertyNames</code> 不会获取到 {{jsxref("Error")}} 对象的属性。该 bug 在后面的版本修复了 ({{bug("724768")}})。</p>
+
+<h2 id="See_also" name="See_also" style="margin-bottom: 20px; line-height: 30px;">相关链接</h2>
+
+<ul>
+ <li><a href="https://developer.mozilla.org/en-US/docs/Enumerability_and_ownership_of_properties" title="Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
+ <li>{{jsxref("Object.prototype.hasOwnProperty")}}</li>
+ <li>{{jsxref("Object.prototype.propertyIsEnumerable")}}</li>
+ <li>{{jsxref("Object.create")}}</li>
+ <li>{{jsxref("Object.keys")}}</li>
+ <li>{{jsxref("Array.forEach()")}}</li>
+</ul>