美文网首页
js object 方法

js object 方法

作者: 前端陈陈陈 | 来源:发表于2021-12-02 15:58 被阅读0次

1、Object.prototype.toString
数组、对象判断具体的类型

Object.prototype.toString.call(abc)  //[object Object]
Object.prototype.toString.call(item) //[object Array]

2、Object.assign()
方法用于将所有可枚举属性的值从一个或多个源对象分配到目标对象。它将返回目标对象。

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }

3、Object.defineProperties()

var obj = {};
Object.defineProperties(obj, {
  'property1': {
    value: true,
    writable: true
  },
  'property2': {
    value: 'Hello',
    writable: false
  }
  // etc. etc.
});

obj:{property1: true, property2: "Hello"}

4、Object.defineProperty()

const object1 = {};

Object.defineProperty(object1, 'property1', {
  value: 42,
  writable: false
});

object1.property1 = 77;
// throws an error in strict mode

console.log(object1.property1);
// expected output: 42

[https://www.jianshu.com/p/8fe1382ba135]

5、Object.fromEntries()
Object.fromEntries() 方法把键值对列表转换为一个对象。

//  Map 转化为 Object

const entries = new Map([
  ['foo', 'bar'],
  ['baz', 42]
]);

const obj = Object.fromEntries(entries);

console.log(obj);
// expected output: Object { foo: "bar", baz: 42 }

// Array 转化为 Object
const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
const obj = Object.fromEntries(arr);
console.log(obj); // { 0: "a", 1: "b", 2: "c" }

6、Object.is()
**Object.is()** 方法判断两个值是否为同一个值

Object.is('foo', 'foo');     // true
Object.is(window, window);   // true

Object.is('foo', 'bar');     // false
Object.is([], []);           // false

var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo);         // true
Object.is(foo, bar);         // false

Object.is(null, null);       // true

// 特例
Object.is(0, -0);            // false
Object.is(0, +0);            // true
Object.is(-0, -0);           // true
Object.is(NaN, 0/0);         // true

相关文章

  • js与ES6对象常用方法区别

    js与ES6对象常用方法区别js中对象方法 Object.assign//用于克隆 Object.is()…用于判...

  • js中Object.defineProperty()方法的解释

    js中Object.defineProperty()方法的解释 菜菜: “老大,那个, Object.define...

  • js object方法

    按常用排序 1、Object.keys(obj) 返回对象的key名数组返回一个由一个给定对象的自身可枚举属性组成...

  • js object 方法

    1、Object.prototype.toString数组、对象判断具体的类型 2、Object.assign()...

  • js拼接html中onclick方法传对象

    js拼接html中方法传参对象func('obj') 防止变成 func('[object object]

  • hasOwnProperty()

    一、hasOwnProperty() js原生方法, object.prototype.hasOwnPropert...

  • 快速将字符串变成数组

    方法一:构造函数 方法二:js方法(split) 方法三:Array.from() 方法四:Object.valu...

  • 2019-03-22

    js字符串转译为unicode的方法: unicode解码方法 js对象(object)变成url的形式 列表的排序

  • js Object 常用方法

    Object.assign(target,source1,source2,...) 该方法主要用于对象的合并,将源...

  • JS:Object.keys()方法

    JS: Object.keys()方法 Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组...

网友评论

      本文标题:js object 方法

      本文链接:https://www.haomeiwen.com/subject/isdyxrtx.html