assign复制对象
In JavaScript, the Object
data type is used to store key value pairs, and like the Array
data type, contain many useful methods. These are some useful methods you'll use while working with objects.
在JavaScript中, Object
数据类型用于存储键值对,并且与Array
数据类型一样,包含许多有用的方法。 这些是在处理对象时将使用的一些有用方法。
对象分配方法 (Object Assign Method)
The Object.assign()
method is used to
Object.assign()
方法用于
- add properties and values to an existing object 向现有对象添加属性和值
- make a new copy of an existing object, or 制作现有对象的新副本,或
- combine multiple existing objects into a single object. 将多个现有对象合并为一个对象。
The Object.assign()
method requires one targetObject
as a parameter and can accept an unlimited number of sourceObjects
as additional parameters.
Object.assign()
方法需要一个targetObject
作为参数,并且可以接受无限数量的sourceObjects
作为附加参数。
It's important to note here is that the targetObject
parameter will always be modified. If that parameter points to an existing object, then that object will be both modified and copied.
这里要注意的重要一点是,始终会修改targetObject
参数。 如果该参数指向现有对象,则将修改和复制该对象。
If, you wish to create a copy of an object without modifying that original object, you can pass an empty object {}
as the first (targetObject
) parameter and the object to be copied as the second (sourceObject
) parameter.
如果要创建对象的副本而不修改原始对象,则可以将空对象{}
作为第一个( targetObject
)参数传递,将要复制的对象作为第二个( sourceObject
)参数传递。
If objects passed as parameters into Object.assign()
share the same properties (or keys), property values that come later in the parameters list will overwrite those which came earlier.
如果作为参数传递给Object.assign()
共享相同的属性(或键),则参数列表中稍后出现的属性值将覆盖之前出现的那些属性值。
Syntax
句法
Object.assign(targetObject, ...sourceObject);
Return Value
返回值
Object.assign()
returns the targetObject
.
Object.assign()
返回targetObject
。
例子 (Examples)
Modifying and copying targetObject
:
修改和复制targetObject
:
let obj = {name: 'Dave', age: 30};
let objCopy = Object.assign(obj, {coder: true});
console.log(obj); // { name: 'Dave', age: 30, coder: true }
console.log(objCopy); // { name: 'Dave', age: 30, coder: true }
Copying targetObject
without modification:
复制targetObject
而不进行修改:
let obj = {name: 'Dave', age: 30};
let objCopy = Object.assign({}, obj, {coder: true});
console.log(obj); // { name: 'Dave', age: 30 }
console.log(objCopy); // { name: 'Dave', age: 30, coder: true }
Objects with the same properties:
具有相同属性的对象:
let obj = {name: 'Dave', age: 30, favoriteColor: 'blue'};
let objCopy = Object.assign({}, obj, {coder: true, favoriteColor: 'red'});
console.log(obj); // { name: 'Dave', age: 30, favoriteColor: 'blue' }
console.log(objCopy); // { name: 'Dave', age: 30, favoriteColor: 'red', coder: true }
对象值方法 (Object Values Method)
The Object.values()
method takes an object as a parameter and returns an array of its values. This makes it useful for chaining with common Array
methods like .map()
, .forEach()
, and .reduce()
.
Object.values()
方法将一个对象作为参数并返回其值的数组。 这使得用于与共同链接有用Array
的方法,如.map()
.forEach()
和.reduce()
Syntax
句法
Object.values(targetObject);
Return value
返回值
An array of the passed object's (targetObject
) values.
传递的对象( targetObject
)值的数组。
例子 (Examples)
const obj = {
firstName: 'Quincy',
lastName: 'Larson'
}
const values = Object.values(obj);
console.log(values); // ["Quincy", "Larson"]
If the object you're passing has numbers as keys, then Object.value()
will return the values according to the numerical order of the keys:
如果您传递的对象具有数字作为键,则Object.value()
将根据键的数字顺序返回值:
const obj1 = { 0: 'first', 1: 'second', 2: 'third' };
const obj2 = { 100: 'apple', 12: 'banana', 29: 'pear' };
console.log(Object.values(obj1)); // ["first", "second", "third"]
console.log(Object.values(obj2)); // ["banana", "pear", "apple"]
If something other than an object is passed to Object.values()
, it will be coerced into an object before being returned as an array:
如果将除对象以外的其他内容传递给Object.values()
,则在将其作为数组返回之前,将其强制转换为对象:
const str = 'hello';
console.log(Object.values(str)); // ["h", "e", "l", "l", "o"]
对象hasOwnProperty方法 (Object hasOwnProperty Method)
The Object.hasOwnProperty()
method returns a boolean indicating if the object owns the specified property.
Object.hasOwnProperty()
方法返回一个布尔值,指示对象是否拥有指定的属性。
This is a convenient method to check if an object has the specified property or not since it returns true/false accordingly.
这是一种检查对象是否具有指定属性的便捷方法,因为它会相应地返回true / false。
Syntax
句法
Object.hasOwnProperty(prop)
Object.hasOwnProperty(prop)
Return value
返回值
true
// or
false
例子 (Examples)
Using Object.hasOwnProperty()
to test if a property exist or not in a given object:
使用Object.hasOwnProperty()
测试给定对象中是否存在属性:
const course = {
name: 'freeCodeCamp',
feature: 'is awesome',
}
const student = {
name: 'enthusiastic student',
}
course.hasOwnProperty('name'); // returns true
course.hasOwnProperty('feature'); // returns true
student.hasOwnProperty('name'); // returns true
student.hasOwnProperty('feature'); // returns false
Object getOwnPropertyNames方法 (Object getOwnPropertyNames Method)
The Object.getOwnPropertyNames()
method takes an object as a parameter and returns and array of all its properties.
Object.getOwnPropertyNames()
方法将对象作为参数,并返回其所有属性的数组。
Syntax
句法
Object.getOwnPropertyNames(obj)
Return value
返回值
An array of strings of the passed object's properties.
传递的对象的属性的字符串数组。
例子 (Examples)
const obj = { firstName: 'Quincy', lastName: 'Larson' }
console.log(Object.getOwnPropertyNames(obj)); // ["firstName", "lastName"]
If something other than an object is passed to Object.getOwnPropertyNames()
, it will be coerced into an object before being returned as an array:
如果将对象以外的东西传递给Object.getOwnPropertyNames()
,则在将其作为数组返回之前,将其强制转换为对象:
const arr = ['1', '2', '3'];
console.log(Object.getOwnPropertyNames(arr)); // ["0", "1", "2", "length"]
然后承诺原型 (Promise.prototype.then)
A Promise.prototype.then
function accepts two arguments and returns a Promise.
Promise.prototype.then
函数接受两个参数并返回Promise。
The first argument is a required function that accepts one argument. Successful fulfillment of a Promise will trigger this function.
第一个参数是接受一个参数的必需函数。 成功履行承诺将触发此功能。
The second argument is an optional function that also accepts one argument of its own. A thrown Error or Rejection of a Promise will trigger this function.
第二个参数是一个可选函数,它也接受自己的一个参数。 抛出错误或拒绝承诺将触发此功能。
function onResolved (resolvedValue) {
/*
* access to resolved values of promise
*/
}
function onRejected(rejectedReason) {
/*
* access to rejection reasons of promise
*/
}
promiseReturningFunction(paramList)
.then( // then function
onResolved,
[onRejected]
);
Promise.prototype.then
allows you to perform many asynchronous activities in sequence. You do this by attaching one then
function to another separated by a dot operator.
Promise.prototype.then
允许您Promise.prototype.then
执行许多异步活动。 通过将一个then
函数附加到另一个由点运算符分隔的函数中,可以做到这一点。
promiseReturningFunction(paramList)
.then( // first then function
function(arg1) {
// ...
return someValue;
}
)
...
.then( // nth then function
function(arg2) {
// ...
return otherValue;
}
)
Map.prototype.entries (Map.prototype.entries)
Returns a new Iterator
object that contains the [key, value]
pairs for each element in the Map
object in insertion order.
返回一个新的Iterator
对象,该对象包含按插入顺序的Map
对象中每个元素的[key, value]
对。
句法 (Syntax)
myMap.entries()
例 (Example)
const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);
var iterator = myMap.entries();
console.log(iterator.next().value); // ['foo', 1]
console.log(iterator.next().value); // ['bar', 2]
console.log(iterator.next().value); // ['baz', 3]
有关JavaScript中对象的更多信息: (More info on objects in JavaScript:)
有关布尔值的更多信息: (More info about booleans:)
assign复制对象