From 310fd066e91f454b990372ffa30e803cc8120975 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 12:56:40 +0100 Subject: unslug zh-cn: move --- .../global_objects/object/prototype/index.html | 195 --------------------- 1 file changed, 195 deletions(-) delete mode 100644 files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/object') diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html deleted file mode 100644 index 4dd70200f0..0000000000 --- a/files/zh-cn/web/javascript/reference/global_objects/object/prototype/index.html +++ /dev/null @@ -1,195 +0,0 @@ ---- -title: Object.prototype -slug: Web/JavaScript/Reference/Global_Objects/Object/prototype -tags: - - JavaScript - - Object - - Property -translation_of: Web/JavaScript/Reference/Global_Objects/Object -translation_of_original: Web/JavaScript/Reference/Global_Objects/Object/prototype ---- -
{{JSRef}}
- -

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原型,会通过原型链改变所有对象;除非在原型链中进一步覆盖受这些变化影响的属性和方法。这提供了一个非常强大的、但有潜在危险的机制来覆盖或扩展对象行为。

- -

属性

- -
-
{{jsxref("Object.prototype.constructor")}}
-
特定的函数,用于创建一个对象的原型。
-
{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}
-
指向当对象被实例化的时候,用作原型的对象。
-
{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}
-
当未定义的对象成员被调用作方法的时候,允许定义并执行的函数。
-
{{jsxref("Object.prototype.__count__")}} {{obsolete_inline}}
-
用于直接返回用户定义的对象中可数的属性的数量。已被废除。
-
{{jsxref("Object.prototype.__parent__")}} {{obsolete_inline}}
-
用于指向对象的内容。已被废除。
-
- -

方法

- -
-
{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
关联一个函数到一个属性。访问该函数时,执行该函数并返回其返回值。
-
{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
关联一个函数到一个属性。设置该函数时,执行该修改属性的函数。
-
{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
返回使用 {{jsxref("Object.defineGetter", "__defineGetter__")}} 定义的方法函数 。
-
{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}
-
返回使用 {{jsxref("Object.defineSetter", "__defineSetter__")}} 定义的方法函数。
-
{{jsxref("Object.prototype.hasOwnProperty()")}}
-
返回一个布尔值 ,表示某个对象是否含有指定的属性,而且此属性非原型链继承的。
-
{{jsxref("Object.prototype.isPrototypeOf()")}}
-
返回一个布尔值,表示指定的对象是否在本对象的原型链中。
-
{{jsxref("Object.prototype.propertyIsEnumerable()")}}
-
判断指定属性是否可枚举,内部属性设置参见 ECMAScript [[Enumerable]] attribute
-
{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}
-
返回字符串表示此对象的源代码形式,可以使用此字符串生成一个新的相同的对象。
-
{{jsxref("Object.prototype.toLocaleString()")}}
-
直接调用 {{jsxref("Object.toString", "toString()")}}方法。
-
{{jsxref("Object.prototype.toString()")}}
-
返回对象的字符串表示。
-
{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}
-
移除对象某个属性的监听。
-
{{jsxref("Object.prototype.valueOf()")}}
-
返回指定对象的原始值。
-
{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}
-
给对象的某个属性增加监听。
-
{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}
-
在指定对象为上下文情况下执行javascript字符串代码,已经废弃。
-
- -

示例

- -

当改变现有的 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();
-
- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{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")}}

- -

相关链接

- - -- cgit v1.2.3-54-g00ecf