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复制对象

assign复制对象_JavaScript标准对象:assign,values,hasOwnProperty和getOwnPropertyNames方法介绍...相关推荐

  1. JavaScript 复制对象与Object.assign方法无法实现深复制

    在JavaScript这门语言中,数据类型分为两大类:基本数据类型和复杂数据类型.基本数据类型包括Number.Boolean.String.Null.String.Symbol(ES6 新增),而复 ...

  2. AutoCAD .Net 不同文档间复制对象

    使用Clone()函数可以在同一 AutoCAD 文档间复制对象. 在不同的 AutoCAD 文档间复制对象,则需要调用Database类的方法WblockCloneObjects. 以下代码演示,将 ...

  3. 在不同的 AutoCAD 文档间复制对象

    /* 使用 Clone()函数可以在同一 AutoCAD 文档间复制对象. 在不同的 AutoCAD 文档间复制对象,则需要调用Database类的方法WblockCloneObjects. 以下代码 ...

  4. JS:js中的复制对象值问题——Object.assign()

    在复制对象的值的时候,往往不能直接"=",这样会造成引用赋值,应该利用一些函数进行对象的复制值.如下: $scope.updateDeliveryOrder = function( ...

  5. js中复制对象的属性值给新的对象

    我们有一个对象,且包含很多属性值和方法,但是我们想把它的内部属性复制给一个新的对象时,我们如何去做呢? 你可能会说直接 a = b就可以了. no no no,这样两个对象其实指针指向的还是一个内存中 ...

  6. 在 JavaScript 中复制对象

    ​​​​​​ 1.JavaScript中的浅拷贝对象 2.深度复制JavaScript中的对象 各种编程语言具有各种数据结构,允许你在内存中组织和存储数据.每个数据结构的工作方式都是独一无二的.例如, ...

  7. JS如何深度复制对象和数组,避免指针变量引用修改值

    //自定义深度复制对象or数组的递归方法---------------------------------------- let copyObjOrArr = o => {let isArray ...

  8. java 复制对象有哪些方式

    2019独角兽企业重金招聘Python工程师标准>>> java 复制对象有哪些方式 Apache的 Common beanutils库 org.apache.commons.bea ...

  9. UNITY 复制对象后局部坐标和世界坐标的变化问题

    UNITY 复制对象后局部坐标和世界坐标的变化问题 void Start () { var pgo = transform.Find ("Button").gameObject; ...

最新文章

  1. mongodb线程池_常用高并发网络线程模型设计及MongoDB线程模型优化实践
  2. 常见的CSS属性和值CascadingStyleSheets
  3. static在内存层面的作用_虚拟地址空间--用户进程看到的自己所占用的内存
  4. java客户端服务器代码_Java Socket通信 客户端服务器端基本代码
  5. SAP CRM中间件调试的一些重要方法
  6. 内核 读写 flash mtd_2D动作卷轴《Lost Epic》公布 少女外表魂系内核|游民星空
  7. 数字图像处理技术在TWaver可视化中的应用
  8. C++ 学习之旅(10)——static与extern
  9. java 如何将word 转换为ftl_使用 freemarker导出word文档
  10. 乐佰小迪智能机器人_云知声 AI 陪伴教育机器人方案亮相广州国际玩具展
  11. 2021年,不平凡的一年~
  12. 中国家禽细菌学诊断行业市场供需与战略研究报告
  13. 真牛!打开mysql
  14. 通过CSS3 Media Query实现响应式Web设计
  15. 君正T31 ACC解码
  16. 学习如何使用css给数字加圆圈效果
  17. PNG图片转CAD图纸有什么快速又方便的方法呢?
  18. 龚文祥自爆今日头条微博自媒体年收入仅1000元!
  19. 阿里云ACP认证(SLB专项)
  20. Mac下修复Node版本不正确的问题

热门文章

  1. C语言指针转换为intptr_t类型
  2. 【Leetcode】33. 搜索旋转排序数组
  3. 程序员必看!Android面试10大知识点总结宝典助你通关!年薪50W
  4. 104 权限 sudo 解压缩
  5. BZOJ 2818 Gcd
  6. BZOJ 1113: [Poi2008]海报PLA
  7. Jenkins --SVN
  8. 关于Unity3D中函数说明
  9. RUNOOB python练习题12 找素数问题
  10. 164. Maximum Gap