JavaScript 对象方法示例

javascript 对象方法示例

javascript 对象方法示例。

object.keys(obj): 返回对象自己的可枚举属性名称(键)的数组。

const obj = { a: 1, b: 2, c: 3 };console.log(object.keys(obj));// output: ['a', 'b', 'c']

登录后复制object.values(obj): 返回对象自己的可枚举属性值的数组。

const obj = { a: 1, b: 2, c: 3 };console.log(object.values(obj));// output: [1, 2, 3]

登录后复制object.entries(obj): 返回对象自己的可枚举字符串键控属性 [key, value] 对的数组。

const obj = { a: 1, b: 2, c: 3 };console.log(object.entries(obj));// output: [['a', 1], ['b', 2], ['c', 3]]

登录后复制object.issealed(obj): 如果对象是密封的,则返回 true,否则返回 false。

const obj = object.seal({ a: 1 });console.log(object.issealed(obj));// output: true

登录后复制object.assign(target, source): 将所有可枚举属性的值从一个或多个源对象复制到目标对象。它返回目标对象。

const target = { a: 1 };const source = { b: 2, c: 3 };const result = object.assign(target, source);console.log(result);// output: { a: 1, b: 2, c: 3 }

登录后复制object.freeze(obj): 冻结对象,防止添加新属性或删除或重新配置现有属性。

const obj = { name: 'khabib' };object.freeze(obj);obj.name = 'bob'; // this won't change the valueconsole.log(obj.name); // output: 'khabib'

登录后复制object.seal(obj): 密封对象,防止添加新属性,但允许修改现有属性。

const obj = { name: 'alice' };object.seal(obj);obj.name = 'bob'; // this will update the valueobj.age = 25; // this won't add a new propertyconsole.log(obj); // output: { name: 'bob' }

登录后复制object.create(proto): 使用指定的原型对象和属性创建一个新对象。

const person = {greet() {console.log('hello!');}};const student = object.create(person);student.greet();// output: 'hello!'

登录后复制object.defineproperty(obj, prop, detector): 直接在对象上定义新属性或修改现有属性。

const obj = {};object.defineproperty(obj, 'name', {value: 'alice',writable: false });console.log(obj.name); // 'alice'

登录后复制object.defineproperties(obj, props): 定义多个新属性或修改对象的现有属性。

const obj = {};object.defineproperties(obj, {name: { value: 'cormier', writable: false },age: { value: 30, writable: true } });console.log(obj.name); // 'cormier'

登录后复制object.isextensible(obj): 确定对象是否可扩展(即是否可以添加新属性)。

const obj = {};console.log(object.isextensible(obj)); // trueobject.preventextensions(obj);console.log(object.isextensible(obj)); // false

登录后复制object.isfrozen(obj): 确定对象是否被冻结(即不可扩展且所有属性不可写)。

const obj = object.freeze({ name: 'gregor' });console.log(object.isfrozen(obj));// output: true

登录后复制object.hasown(obj, prop): 如果指定对象将指定属性作为其自己的属性,则返回 true,即使该属性的值未定义。

const obj = { name: 'alice' };console.log(object.hasown(obj, 'name')); // trueconsole.log(object.hasown(obj, 'age')); // false

登录后复制object.hasownproperty(prop): 确定对象是否包含指定属性作为该对象的直接属性,而不是通过原型链继承。

const obj = { name: 'alice' };console.log(obj.hasownproperty('name')); // trueconsole.log(obj.hasownproperty('age')); // false

登录后复制object.preventextensions(obj): 防止将新属性添加到对象中。

const obj = {};object.preventextensions(obj);obj.name = 'khabib'; // won't be addedconsole.log(obj); // {}

登录后复制object.setprototypeof(obj, proto): 设置指定对象的原型(内部 [[prototype]] 属性)。

const proto = { greet() {console.log('hello!');}};const obj = {};object.setprototypeof(obj, proto);obj.greet(); // 'hello!'

登录后复制object.fromentries(iterable):键值对列表转换为对象。

const entries = [['name', 'rock'], ['age', 35]];const obj = object.fromentries(entries);console.log(obj); // { name: 'rock', age: 35 }

登录后复制object.getprototypeof(obj):返回指定对象的原型(内部 [[prototype]] 属性)。

const obj = {};const proto = object.getprototypeof(obj);console.log(proto === object.prototype); // true

登录后复制object.getownpropertysymbols(obj): 返回在对象上找到的所有符号属性的数组。

const symbol = symbol('id');const obj = { [symbol]: 123 };const symbols = object.getownpropertysymbols(obj);console.log(symbols); // [symbol(id)]console.log(obj[symbols[0]]); // 123

登录后复制object.getownpropertydescriptor(obj, prop): 返回给定对象的特定属性的属性描述符。

const obj = { name: 'alice', age: 26 };const descriptor = object.getownpropertydescriptor(obj, 'name');console.log(descriptor);// output: { configurable: true, enumerable: true, value: "alice", writable: true }

登录后复制object.getownpropertynames(obj): 返回在对象上找到的所有属性(包括不可枚举属性)的数组。9688635538​​35object.is(value1, value2): 比较两个值是否相同。

console.log(object.is('foo', 'foo')); // trueconsole.log(object.is({}, {})); // false

登录后复制object.getownpropertydescriptors(obj): 返回对象的所有自有属性描述符。

const obj = { name: 'Khabib', age: 28 };const descriptors = Object.getOwnPropertyDescriptors(obj);console.log(descriptors);// Output: {age: {configurable: true,enumerable: true,value: 28,writable: true },name: {  configurable: true,  enumerable: true,  value: "Khabib",  writable: true }}

登录后复制

以上就是JavaScript 对象方法示例的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2658144.html

(0)
上一篇 2025年3月7日 10:16:20
下一篇 2025年2月18日 13:25:01

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论