From 4a454402ad5e6feeca510b0e7e84c6af49a91dce Mon Sep 17 00:00:00 2001 From: 现阳 <43157411+meakle@users.noreply.github.com> Date: Sun, 5 Dec 2021 15:29:43 +0800 Subject: Sync Web/JavaScript/Reference/Global_Objects/Object, zh-CN (#3240) * update: keep Object page same with English docs url: /zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object description: Updated some documents on the Object page, a small part of the content can not guarantee the quality of translation, so no translation * fix: review --- .../reference/global_objects/object/index.html | 191 +++++++++++++++++---- 1 file changed, 159 insertions(+), 32 deletions(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/index.html index c35c5e8fa9..cd90b70b07 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/object/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/object/index.html @@ -11,28 +11,19 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Object ---
Object
构造函数创建一个对象包装器。
// 对象初始化器(Object initialiser)或对象字面量(literal) -{ [ nameValuePair1[, nameValuePair2[, ...nameValuePairN] ] ] } - -// 以构造函数形式来调用 -new Object([value])- -
nameValuePair1, nameValuePair2, ... nameValuePairN
value
Object
是 JavaScript 的一种 数据类型 。它用于存储各种键值集合和更复杂的实体。Objects 可以通过 {{jsxref("Object.prototype.Object", "Object()")}}
构造函数或者使用 对象字面量 的方式创建
在JavaScript中,几乎所有的对象都是Object
类型的实例,它们都会从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
构造函数的属性Object.length
Object 自身没有提供方法删除其自身属性(Map 中的 {{jsxref("Map.prototype.delete()")}}
可以删除自身属性 )为了删除对象上的属性,必须使用 delete 操作符
Object
对象。该对象将会包裹(wrapper)传入的参数 Object
实例和 Object
原型对象JavaScript中的所有对象都来自 Object
;所有对象从{{jsxref("Object.prototype")}}继承方法和属性,尽管它们可能被覆盖。例如,其他构造函数的原型将覆盖 constructor
属性并提供自己的 toString()
方法。Object
原型对象的更改将传播到所有对象,除非受到这些更改的属性和方法将沿原型链进一步覆盖。
{{jsxref("Object.prototype.__defineGetter__", "Object.prototype.__defineGetter__()")}}
得出。{{jsxref("Object.prototype.__defineSetter__", "Object.prototype.__defineSetter__()")}}
得出。toString()。
+ 当我们要修改现有的 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(); + + ++