Object.prototype 属性表示 Object 的原型对象。
Object.prototype 属性的属性特性: |
|
|---|---|
| writable | true |
| enumerable | false |
| configurable | true |
几乎所有的 JavaScript 对象都是 Object 的实例;一个典型的对象继承了Object.prototype的属性(包括方法),尽管这些属性可能被遮蔽(亦称为覆盖)。但是有时候可能故意创建不具有典型原型链继承的对象,比如通过Object.create(null)创建的对象,或者通过Object.setPrototypeOf方法改变原型链。
改变Object原型,会通过原型链改变所有对象;除非在原型链中进一步覆盖受这些变化影响的属性和方法。这提供了一个非常强大的、但有潜在危险的机制来覆盖或扩展对象行为。
Object.prototype.constructorObject.prototype.__proto__ Object.prototype.__noSuchMethod__ Object.prototype.__count__ Object.prototype.__parent__ Object.prototype.__defineGetter__() Object.prototype.__defineSetter__() Object.prototype.__lookupGetter__() __defineGetter__ 定义的方法函数 。Object.prototype.__lookupSetter__() __defineSetter__ 定义的方法函数。Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toSource() Object.prototype.toLocaleString()toString()方法。Object.prototype.toString()Object.prototype.unwatch() Object.prototype.valueOf()Object.prototype.watch() Object.prototype.eval() 当改变现有的 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 |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
| ECMAScript 5.1 (ECMA-262) Object.prototype |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) Object.prototype |
Standard | |
| ECMAScript Latest Draft (ECMA-262) Object.prototype |
Draft |
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
prototype | Chrome Full support Yes | Edge Full support Yes | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |