--- title: Object.isExtensible() slug: Web/JavaScript/Reference/Global_Objects/Object/isExtensible translation_of: Web/JavaScript/Reference/Global_Objects/Object/isExtensible ---
{{JSRef("Global_Objects", "Object")}}

概述

Object.isExtensible() 方法判断一个对象是否是可扩展的(是否可以在它上面添加新的属性)。

语法

Object.isExtensible(obj)

参数

obj
需要检测的对象

返回值

    表示给定对象是否可扩展的一个Boolean 。

描述

默认情况下,对象是可扩展的:即可以为他们添加新的属性。以及它们的 {{jsxref("Object.proto", "__proto__")}}{{deprecated_inline}} 属性可以被更改。{{jsxref("Object.preventExtensions")}},{{jsxref("Object.seal")}} 或 {{jsxref("Object.freeze")}} 方法都可以标记一个对象为不可扩展(non-extensible)。

例子

// 新对象默认是可扩展的.
var empty = {};
Object.isExtensible(empty); // === true

// ...可以变的不可扩展.
Object.preventExtensions(empty);
Object.isExtensible(empty); // === false

// 密封对象是不可扩展的.
var sealed = Object.seal({});
Object.isExtensible(sealed); // === false

// 冻结对象也是不可扩展.
var frozen = Object.freeze({});
Object.isExtensible(frozen); // === false

 

注意

在 ES5 中,如果参数不是一个对象类型,将抛出一个 {{jsxref("TypeError")}} 异常。在 ES6 中, non-object 参数将被视为一个不可扩展的普通对象,因此会返回 false 。

Object.isExtensible(1);
// TypeError: 1 is not an object (ES5 code)

Object.isExtensible(1);
// false                         (ES6 code)

规范

Specification Status Comment
{{SpecName('ES5.1', '#sec-15.2.3.13', 'Object.isExtensible')}} {{Spec2('ES5.1')}} Initial definition.
Implemented in JavaScript 1.8.5
{{SpecName('ES6', '#sec-object.isextensible', 'Object.isExtensible')}} {{Spec2('ES6')}}  

浏览器兼容性

{{CompatibilityTable}}
Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support 4 (2.0) 6 9 12 5.1
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

相关链接