--- title: Object slug: Web/JavaScript/Reference/Global_Objects/Object tags: - Constructor - JavaScript - Object - 对象 - 构造器 translation_of: Web/JavaScript/Reference/Global_Objects/Object ---
Object
是 JavaScript 的一种 数据类型 。它用于存储各种键值集合和更复杂的实体。Objects 可以通过 {{jsxref("Object.prototype.Object", "Object()")}}
构造函数或者使用 对象字面量 的方式创建
在JavaScript中,几乎所有的对象都是Object
类型的实例,它们都会从Object.prototype
继承属性和方法,虽然大部分属性都会被覆盖(shadowed)或者说被重写了(overridden)。
除此之外,Object
还可以被故意的创建,但是这个对象并不是一个“真正的对象”(例如:通过 {{jsxref("Object.create()", "Object.create(null)")}}
),或者通过一些手段改变对象,使其不再是一个“真正的对象”(比如说: {{jsxref("Object.setPrototypeOf")}}
)。
通过原型链,所有的 object
都能观察到 Object 原型对象(Object prototype object)的改变,除非这些受到改变影响的属性和方法沿着原型链被进一步的重写。尽管有潜在的危险,但这为覆盖或扩展对象的行为提供了一个非常强大的机制。
Object
构造函数为给定的参数创建一个包装类对象(object wrapper),具体有以下情况:
当以非构造函数形式被调用时,Object
的行为等同于 new Object()
。
可查看 对象初始化/字面量语法。
Object 自身没有提供方法删除其自身属性(Map 中的 {{jsxref("Map.prototype.delete()")}}
可以删除自身属性 )为了删除对象上的属性,必须使用 delete 操作符
Object
对象。该对象将会包裹(wrapper)传入的参数 [key, value]
数组。[[Prototype]]
属性)。{{jsxref("Object.prototype.__defineGetter__", "Object.prototype.__defineGetter__()")}}
得出。{{jsxref("Object.prototype.__defineSetter__", "Object.prototype.__defineSetter__()")}}
得出。toString()。
undefined
和 null
类型使用 Object
下面的例子将一个空的 Object
对象存到 o
中:
var o = new Object();
var o = new Object(undefined);
var o = new Object(null);
Object
生成布尔对象下面的例子将{{jsxref("Boolean")}} 对象存到 o
中:
// 等价于 o = new Boolean(true); var o = new Object(true);
// 等价于 o = new Boolean(false); var o = new Object(Boolean());
当我们要修改现有的 Object.prototype
方法时,请你考虑一下在已经存在的逻辑之前或者之后通过包装扩展代码的方式来注入代码。
比如说,有一段代码将会在执行内部逻辑或者是其他扩展之前, 有条件的执行一段自定义的逻辑。
当一个函数被调用时,调用的参数被保存在一个类似数组的“变量” arguments。
比如说:在调用 myFn(a, b, c)
时,myFunc 函数体中的 arguments 将会包含三个类似数组的元素,对应 (a, b , c)
When modifying prototypes with hooks, pass this
and the arguments (the call state) to the current behavior by calling apply()
on the function. This pattern can be used for any prototype, such as Node.prototype
, Function.prototype
, etc.
var current = Object.prototype.valueOf; // Since my property "-prop-value" is cross-cutting and isn't always // on the same prototype chain, I want to modify Object.prototype: Object.prototype.valueOf = function() { if (this.hasOwnProperty('-prop-value')) { return this['-prop-value']; } else { // It doesn't look like one of my objects, so let's fall back on // the default behavior by reproducing the current behavior as best we can. // The apply behaves like "super" in some other languages. // Even though valueOf() doesn't take arguments, some other hook may. return current.apply(this, arguments); } }
Since JavaScript doesn't exactly have sub-class objects, prototype is a useful workaround to make a “base class” object of certain functions that act as objects. For example:
var Person = function(name) { this.name = name; this.canTalk = true; }; Person.prototype.greet = function() { if (this.canTalk) { console.log('Hi, I am ' + this.name); } }; var Employee = function(name, title) { Person.call(this, name); this.title = title; }; Employee.prototype = Object.create(Person.prototype); Employee.prototype.constructor = Employee; //If you don't set Object.prototype.constructor to Employee, //it will take prototype.constructor of Person (parent). //To avoid that, we set the prototype.constructor to Employee (child). Employee.prototype.greet = function() { if (this.canTalk) { console.log('Hi, I am ' + this.name + ', the ' + this.title); } }; var Customer = function(name) { Person.call(this, name); }; Customer.prototype = Object.create(Person.prototype); Customer.prototype.constructor = Customer; //If you don't set Object.prototype.constructor to Customer, //it will take prototype.constructor of Person (parent). //To avoid that, we set the prototype.constructor to Customer (child). var Mime = function(name) { Person.call(this, name); this.canTalk = false; }; Mime.prototype = Object.create(Person.prototype); Mime.prototype.constructor = Mime; //If you don't set Object.prototype.constructor to Mime, //it will take prototype.constructor of Person (parent). //To avoid that, we set the prototype.constructor to Mime (child). var bob = new Employee('Bob', 'Builder'); var joe = new Customer('Joe'); var rg = new Employee('Red Green', 'Handyman'); var mike = new Customer('Mike'); var mime = new Mime('Mime'); bob.greet(); // Hi, I am Bob, the Builder joe.greet(); // Hi, I am Joe rg.greet(); // Hi, I am Red Green, the Handyman mike.greet(); // Hi, I am Mike mime.greet();
Specification | Status | Comment |
---|---|---|
{{SpecName('ES1')}} | {{Spec2('ES1')}} | Initial definition. Implemented in JavaScript 1.0. |
{{SpecName('ES5.1', '#sec-15.2', 'Object')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-object-objects', 'Object')}} | {{Spec2('ES6')}} | Added Object.assign, Object.getOwnPropertySymbols, Object.setPrototypeOf, Object.is |
{{SpecName('ESDraft', '#sec-object-objects', 'Object')}} | {{Spec2('ESDraft')}} | Added Object.entries and Object.values. |
{{Compat("javascript.builtins.Object")}}