javascript
assign复制对象_JavaScript标准对象:assign,values,hasOwnProperty和getOwnPropertyNames方法介绍...
assign復(fù)制對(duì)象
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數(shù)據(jù)類(lèi)型用于存儲(chǔ)鍵值對(duì),并且與Array數(shù)據(jù)類(lèi)型一樣,包含許多有用的方法。 這些是在處理對(duì)象時(shí)將使用的一些有用方法。
對(duì)象分配方法 (Object Assign Method)
The Object.assign() method is used to
Object.assign()方法用于
The Object.assign() method requires one targetObject as a parameter and can accept an unlimited number of sourceObjects as additional parameters.
Object.assign()方法需要一個(gè)targetObject作為參數(shù),并且可以接受無(wú)限數(shù)量的sourceObjects作為附加參數(shù)。
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.
這里要注意的重要一點(diǎn)是,始終會(huì)修改targetObject參數(shù)。 如果該參數(shù)指向現(xiàn)有對(duì)象,則將修改和復(fù)制該對(duì)象。
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.
如果要?jiǎng)?chuàng)建對(duì)象的副本而不修改原始對(duì)象,則可以將空對(duì)象{}作為第一個(gè)( targetObject )參數(shù)傳遞,將要復(fù)制的對(duì)象作為第二個(gè)( sourceObject )參數(shù)傳遞。
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.
如果作為參數(shù)傳遞給Object.assign()共享相同的屬性(或鍵),則參數(shù)列表中稍后出現(xiàn)的屬性值將覆蓋之前出現(xiàn)的那些屬性值。
Syntax
句法
Object.assign(targetObject, ...sourceObject);Return Value
返回值
Object.assign() returns the targetObject.
Object.assign()返回targetObject 。
例子 (Examples)
Modifying and copying targetObject:
修改和復(fù)制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:
復(fù)制targetObject而不進(jìn)行修改:
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:
具有相同屬性的對(duì)象:
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 }對(duì)象值方法 (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()方法將一個(gè)對(duì)象作為參數(shù)并返回其值的數(shù)組。 這使得用于與共同鏈接有用Array的方法,如.map() .forEach()和.reduce()
Syntax
句法
Object.values(targetObject);Return value
返回值
An array of the passed object's (targetObject) values.
傳遞的對(duì)象( targetObject )值的數(shù)組。
例子 (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:
如果您傳遞的對(duì)象具有數(shù)字作為鍵,則Object.value()將根據(jù)鍵的數(shù)字順序返回值:
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:
如果將除對(duì)象以外的其他內(nèi)容傳遞給Object.values() ,則在將其作為數(shù)組返回之前,將其強(qiáng)制轉(zhuǎn)換為對(duì)象:
const str = 'hello';console.log(Object.values(str)); // ["h", "e", "l", "l", "o"]對(duì)象hasOwnProperty方法 (Object hasOwnProperty Method)
The Object.hasOwnProperty() method returns a boolean indicating if the object owns the specified property.
Object.hasOwnProperty()方法返回一個(gè)布爾值,指示對(duì)象是否擁有指定的屬性。
This is a convenient method to check if an object has the specified property or not since it returns true/false accordingly.
這是一種檢查對(duì)象是否具有指定屬性的便捷方法,因?yàn)樗鼤?huì)相應(yīng)地返回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()測(cè)試給定對(duì)象中是否存在屬性:
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 falseObject getOwnPropertyNames方法 (Object getOwnPropertyNames Method)
The Object.getOwnPropertyNames() method takes an object as a parameter and returns and array of all its properties.
Object.getOwnPropertyNames()方法將對(duì)象作為參數(shù),并返回其所有屬性的數(shù)組。
Syntax
句法
Object.getOwnPropertyNames(obj)Return value
返回值
An array of strings of the passed object's properties.
傳遞的對(duì)象的屬性的字符串?dāng)?shù)組。
例子 (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:
如果將對(duì)象以外的東西傳遞給Object.getOwnPropertyNames() ,則在將其作為數(shù)組返回之前,將其強(qiáng)制轉(zhuǎn)換為對(duì)象:
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函數(shù)接受兩個(gè)參數(shù)并返回Promise。
The first argument is a required function that accepts one argument. Successful fulfillment of a Promise will trigger this function.
第一個(gè)參數(shù)是接受一個(gè)參數(shù)的必需函數(shù)。 成功履行承諾將觸發(fā)此功能。
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.
第二個(gè)參數(shù)是一個(gè)可選函數(shù),它也接受自己的一個(gè)參數(shù)。 拋出錯(cuò)誤或拒絕承諾將觸發(fā)此功能。
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執(zhí)行許多異步活動(dòng)。 通過(guò)將一個(gè)then函數(shù)附加到另一個(gè)由點(diǎn)運(yùn)算符分隔的函數(shù)中,可以做到這一點(diǎn)。
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.
返回一個(gè)新的Iterator對(duì)象,該對(duì)象包含按插入順序的Map對(duì)象中每個(gè)元素的[key, value]對(duì)。
句法 (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]有關(guān)JavaScript中對(duì)象的更多信息: (More info on objects in JavaScript:)
How to create objects in JavaScript
如何在JavaScript中創(chuàng)建對(duì)象
How to loop through objects in JavaScript
如何遍歷JavaScript中的對(duì)象
有關(guān)布爾值的更多信息: (More info about booleans:)
Booleans in JavaScript
JavaScript中的布爾值
翻譯自: https://www.freecodecamp.org/news/javascript-standard-objects-assign-values-hasownproperty-and-getownpropertynames-methods-explained/
assign復(fù)制對(duì)象
總結(jié)
以上是生活随笔為你收集整理的assign复制对象_JavaScript标准对象:assign,values,hasOwnProperty和getOwnPropertyNames方法介绍...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到大葱是什么意思周公解梦
- 下一篇: 梦到朋友追杀我是什么意思