--- title: Object.prototype slug: conflicting/Web/JavaScript/Reference/Global_Objects/Object tags: - JavaScript - Object - Property translation_of: Web/JavaScript/Reference/Global_Objects/Object translation_of_original: Web/JavaScript/Reference/Global_Objects/Object/prototype original_slug: Web/JavaScript/Reference/Global_Objects/Object/prototype ---
Object.prototype 属性表示 {{jsxref("Object")}} 的原型对象。
{{js_property_attributes(0, 0, 0)}}
几乎所有的 JavaScript 对象都是 {{jsxref("Object")}} 的实例;一个典型的对象继承了Object.prototype的属性(包括方法),尽管这些属性可能被遮蔽(亦称为覆盖)。但是有时候可能故意创建不具有典型原型链继承的对象,比如通过{{jsxref("Object.create", "Object.create(null)")}}创建的对象,或者通过{{jsxref("Object.setPrototypeOf")}}方法改变原型链。
改变Object原型,会通过原型链改变所有对象;除非在原型链中进一步覆盖受这些变化影响的属性和方法。这提供了一个非常强大的、但有潜在危险的机制来覆盖或扩展对象行为。
当改变现有的 Object.prototype method(方法)的行为时,考虑在现有逻辑之前或之后通过封装你的扩展来注入代码。例如,此(未测试的)代码将在内置逻辑或其他人的扩展执行之前 pre-conditionally(预条件地)执行自定义逻辑。
当一个函数被调用时,调用的参数被保留在类似数组 "变量" 的参数中。例如, 在调用 "myFn (a、b、c)"时, 在myFn 的主体内的参数将包含 3个类似数组的元素对应于 (a、b、c)。 使用钩子修改原型时,只需通过调用该函数的 apply (),将 this 与参数 (调用状态) 传递给当前行为。这种模式可以用于任何原型,如 Node.prototype、 Function.prototype 等.
var current = Object.prototype.valueOf;
// 由于我的属性 "-prop-value"是交叉性的, 并不总是
// 在同一个原型链上,我想要修改 Object.prototype:
Object.prototype.valueOf = function() {
if (this.hasOwnProperty('-prop-value')) {
return this['-prop-value'];
} else {
// 它看起来不像我的对象之一,因此,让我们退回到
// 默认行为,通过尽可能地复制当前行为来实现.
// 此apply的行为类似于其他语言中的"super".
// 即使 valueOf() 不带参数, 其他的钩子可能会带有.
return current.apply(this, arguments);
}
}
由于 JavaScript 并不完全具有子类对象, 所以原型是一种有用的变通方法, 可以使用某些函数的 "基类" 对象来充当对象。例如:
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.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);
var Mime = function(name) {
Person.call(this, name);
this.canTalk = false;
};
Mime.prototype = Object.create(Person.prototype);
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.3.1', 'Object.prototype')}} | {{Spec2('ES5.1')}} | |
| {{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}} | {{Spec2('ES6')}} | |
| {{SpecName('ESDraft', '#sec-object.prototype', 'Object.prototype')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Object.prototype")}}