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.mzph.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作…