Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use objects in js

Shulou Source: shulou.com Published: 2022-06-01 04:17:25 09月21日 Update

这篇文章给大家分享的是有关js中对象怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

对象1 使用解构删除不必要属性

有时候你不希望保留某些对象属性,也许是因为它们包含敏感信息或仅仅是太大了(just too big)。你可能会枚举整个对象然后删除它们,但实际上只需要简单的将这些无用属性赋值给变量,然后把想要保留的有用部分作为剩余参数就可以了。

下面的代码里,我们希望删除_internal和tooBig参数。我们可以把它们赋值给internal和tooBig变量,然后在cleanObject中存储剩下的属性以备后用。

let {_internal, tooBig, ...cleanObject} = {el1: '1', _internal:"secret", tooBig:{}, el2: '2', el3: '3'};console.log(cleanObject); // {el1: '1', el2: '2', el3: '3'}2 在函数参数中解构嵌套对象

在下面的代码中,engine是对象car中嵌套的一个对象。如果我们对engine的vin属性感兴趣,使用解构赋值可以很轻松地得到它。

var car = { model: 'bmw 2018', engine: { v6: true, turbo: true, vin: 12345 }}const modelAndVIN = ({model, engine: {vin}}) => { console.log(`model: ${model} vin: ${vin}`);}modelAndVIN(car); // => model: bmw 2018 vin: 12345感谢各位的阅读!关于"js中对象怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Tags: Objects properties parameters code content variables more articles facets good practical information interests functions actually sexy articles sometimes useful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux MySQL MariaDB Docker