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 truestudent.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 functiononResolved,[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 functionfunction(arg1) {// ...return someValue;})....then( // nth then functionfunction(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:)
How to create objects in JavaScript
如何在JavaScript中創建對象
How to loop through objects in JavaScript
如何遍歷JavaScript中的對象
有關布爾值的更多信息: (More info about booleans:)
Booleans in JavaScript
JavaScript中的布爾值
翻譯自: https://www.freecodecamp.org/news/javascript-standard-objects-assign-values-hasownproperty-and-getownpropertynames-methods-explained/
assign復制對象