assign復制對象_JavaScript標準對象:assign,values,hasOwnProperty和getOwnPropertyNames方法介紹...

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()方法用于

  1. add properties and values to an existing object

    向現有對象添加屬性和值
  2. make a new copy of an existing object, or

    制作現有對象的新副本,或
  3. 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復制對象

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390776.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390776.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390776.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HDFS 技術

HDFS定義 Hadoop Distributed File System,是一個使用 Java 實現的、分布式的、可橫向擴展的文件系 統,是 HADOOP 的核心組件 HDFS特點 處理超大文件流式地訪問數據運行于廉價的商用機器集群上; HDFS 不適合以下場合:低延遲數據…

深度學習算法和機器學習算法_啊哈! 4種流行的機器學習算法的片刻

深度學習算法和機器學習算法Most people are either in two camps:大多數人都在兩個營地中: I don’t understand these machine learning algorithms. 我不了解這些機器學習算法。 I understand how the algorithms work, but not why they work. 我理解的算法是如…

Python第一次周考(0402)

2019獨角獸企業重金招聘Python工程師標準>>> 一、單選 1、Python3中下列語句錯誤的有哪些? A s input() B s raw_input() C print(hello world.) D print(hello world.) 2、下面哪個是 Pycharm 在 Windows 下 默認 用于“批量注釋”的快捷鍵 A Ctrl d…

express 路由中間件_Express通過示例進行解釋-安裝,路由,中間件等

express 路由中間件表達 (Express) When it comes to build web applications using Node.js, creating a server can take a lot of time. Over the years Node.js has matured enough due to the support from community. Using Node.js as a backend for web applications a…

ASP.NET 頁面之間傳值的幾種方式

對于任何一個初學者來說,頁面之間傳值可謂是必經之路,卻又是他們的難點。其實,對大部分高手來說,未必不是難點。 回想2016年面試的將近300人中,有實習生,有應屆畢業生,有1-3年經驗的&#xff0c…

Mapreduce原理和YARN

MapReduce定義 MapReduce是一種分布式計算框架,由Google公司2004年首次提出,并貢獻給Apache基金會。 MR版本 MapReduce 1.0,Hadoop早期版本(只支持MR模型)MapReduce 2.0,Hadoop 2.X版本(引入了YARN資源調度框架后&a…

數據可視化圖表類型_數據可視化中12種最常見的圖表類型

數據可視化圖表類型In the current era of large amounts of information in the form of numbers available everywhere, it is a difficult task to understand and get insights from these dense piles of data.在當今時代,到處都是數字形式的大量信息&#xff…

三大紀律七項注意(Access數據庫)

三大紀律(規則或范式) 要有主鍵其他字段依賴主鍵其他字段之間不能依賴七項注意 一表一主鍵(訂單表:訂單號;訂單明細表:訂單號產品編號)經常查,建索引,小數據(日期,數字類…

CentOS下安裝JDK的三種方法

來源:Linux社區 作者:spiders http://www.linuxidc.com/Linux/2016-09/134941.htm 由于各Linux開發廠商的不同,因此不同開發廠商的Linux版本操作細節也不一樣,今天就來說一下CentOS下JDK的安裝: 方法一:手動解壓JDK的壓縮包,然后…

MapReduce編程

自定義Mapper類 class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> &#xff5b; … }自定義mapper類都必須實現Mapper類&#xff0c;有4個類型參數&#xff0c;分別是&#xff1a; Object&#xff1a;Input Key Type-------------K1Text: Input…

統計信息在數據庫中的作用_統計在行業中的作用

統計信息在數據庫中的作用數據科學與機器學習 (DATA SCIENCE AND MACHINE LEARNING) Statistics are everywhere, and most industries rely on statistics and statistical thinking to support their business. The interest to grasp on statistics also required to become…

IOS手機關于音樂自動播放問題的解決辦法

2019獨角獸企業重金招聘Python工程師標準>>> 評估手機自帶瀏覽器不能識別 aduio標簽重的autoplay屬性 也不能自動執行play()方法 一個有效的解決方案是在微信jssdk中調用play方法 document.addEventListener("WeixinJSBridgeReady", function () { docum…

svg標簽和svg文件區別_什么是SVG文件? SVG圖片和標簽說明

svg標簽和svg文件區別SVG (SVG) SVG or Scalable Vector Graphics is a web standard for defining vector-based graphics in web pages. Based on XML the SVG standard provides markup to describe paths, shapes, and text within a viewport. The markup can be embedded…

開發人員怎么看實施人員

英文原文&#xff1a;What Developers Think Of Operations&#xff0c;翻譯&#xff1a;張紅月CSDN 在一個公司里面&#xff0c;開發和產品實施對于IS/IT的使用是至關重要的&#xff0c;一個負責產品的研發工作&#xff0c;另外一個負責產品的安裝、調試等工作。但是在開發人員…

怎么評價兩組數據是否接近_接近組數據(組間)

怎么評價兩組數據是否接近接近組數據(組間) (Approaching group data (between-group)) A typical situation regarding solving an experimental question using a data-driven approach involves several groups that differ in (hopefully) one, sometimes more variables.使…

代碼審計之DocCms漏洞分析

0x01 前言 DocCms[音譯&#xff1a;稻殼Cms] &#xff0c;定位于為企業、站長、開發者、網絡公司、VI策劃設計公司、SEO推廣營銷公司、網站初學者等用戶 量身打造的一款全新企業建站、內容管理系統&#xff0c;服務于企業品牌信息化建設&#xff0c;也適應用個人、門戶網站建設…

你讓,勛爵? 使用Jenkins聲明性管道的Docker中的Docker

Resources. When they are unlimited they are not important. But when theyre limited, boy do you have challenges! 資源。 當它們不受限制時&#xff0c;它們并不重要。 但是&#xff0c;當他們受到限制時&#xff0c;男孩你有挑戰&#xff01; Recently, my team has fa…

翻譯(九)——Clustered Indexes: Stairway to SQL Server Indexes Level 3

原文鏈接&#xff1a;www.sqlservercentral.com/articles/StairwaySeries/72351/ Clustered Indexes: Stairway to SQL Server Indexes Level 3 By David Durant, 2013/01/25 (first published: 2011/06/22) The Series 本文是階梯系列的一部分&#xff1a;SQL Server索引的階梯…

power bi 中計算_Power BI中的期間比較

power bi 中計算Just recently, I’ve come across a question on the LinkedIn platform, if it’s possible to create the following visualization in Power BI:就在最近&#xff0c;我是否在LinkedIn平臺上遇到了一個問題&#xff0c;是否有可能在Power BI中創建以下可視化…

-Hive-

Hive定義 Hive 是一種數據倉庫技術&#xff0c;用于查詢和管理存儲在分布式環境下的大數據集。構建于Hadoop的HDFS和MapReduce上&#xff0c;用于管理和查詢分析結構化/非結構化數據的數據倉庫; 使用HQL&#xff08;類SQL語句&#xff09;作為查詢接口&#xff1b;使用HDFS作…