当前位置:网站首页>Object object
Object object
2022-07-28 23:24:00 【Corner sheep】
List of articles
- Object
- Static methods
- Object.assign()
- Object.create() Method creates a new object using the specified prototype object and its properties .
- Object.defineProperty()
- Properties and internal properties
- property descriptor
- Object.defineProperties()
- Object.entries()
- Object.freeze()
- Object.getOwnPropertyDescriptor()
- Object.getOwnPropertyNames()
- Object.getOwnPropertySymbols()
- Object.getPrototypeOf()
- Object.is()
- Object.isExtensible()
- Object.isFrozen()
- Object.keys()
- Object.setPrototypeOf()
- Object.values()
- Example method
Object
Object yes JavaScript A kind of data type . It is used to store various key value sets and more complex entities .Objects Can pass Object() Constructor or use Object literal The way to create
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object
Static methods
Object.assign()
Object.assign() Method is used to assign the values of all enumerable properties from one or more source objects to the target objects . It will return the target object .
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 }
Object.create() Method creates a new object using the specified prototype object and its properties .
grammar
Object.create(proto[, propertiesObject])
Object.defineProperty()
Method defines a new property directly on an object , Or modify the existing properties of an object , And return this object .
grammar :Object.defineProperty(obj, prop, desc)
obj The current object that needs to define properties
prop Property name to be defined at present
desc property descriptor
Generally, by assigning values to the attributes of the object , The properties of an object can be modified or deleted , But by Object.defineProperty() Defining attributes , Through the setting of descriptors, you can control object properties more accurately .
Properties and internal properties
javacript There are three types of properties
1、 Name data properties : An attribute that has a definite value . This is also the most common attribute
2、 Name accessor properties : adopt getter and setter Properties for reading and assignment
3、 Internal attributes : from JavaScript Properties used inside the engine , Cannot pass JavaScript Code direct access to , However, some methods can be used to indirectly read and set . such as , Each object has an internal property [[Prototype]], You can't access this property directly , But it can go through Object.getPrototypeOf() Method indirectly reads its value . Although internal attributes are usually represented by a name surrounded by double braces , But in fact, this is not their name , They are abstract operations , It's invisible , There is no such string type attribute as the above two attributes
property descriptor
adopt Object.defineProperty() Define properties for objects , There are two forms , And can not be mixed , Data descriptors , Access descriptor , The differences between the two are described below :
// Data descriptor -- Two unique attributes (value,writable)
let Person = {
};
Object.defineProperty(Person, "name", {
value: "jack",
writable: true, // Is it possible to change
});
Object.defineProperties()
Add multiple properties to an object and specify their configuration separately .
Object.defineProperties(obj, props)
Object.entries()
Method returns an array of key value pairs of enumerable properties of a given object , Its arrangement and use for…in Loop through the object in the same order ( The difference lies in for-in The loop also enumerates the properties in the prototype chain ).
const obj = {
foo: "bar", baz: 42 };
console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
Object.freeze()
Method to freeze an object . A frozen object can no longer be modified ; You cannot add new properties to an object when it is frozen , Cannot delete an existing property , Enumerability of existing properties of this object cannot be modified 、 Configurability 、 writability , And the value of an existing property cannot be modified . Besides , When an object is frozen, its prototype cannot be modified .freeze() Returns the same object as the passed in parameter .
const obj = {
prop: 42,
};
Object.freeze(obj);
obj.prop = 33;
// Throws an error in strict mode
console.log(obj.prop);
// expected output: 42
Object.getOwnPropertyDescriptor()
Method returns the property descriptor corresponding to a self owned property on the specified object .( Self owned attribute refers to the attribute directly assigned to the object , Properties that do not need to be looked up from the prototype chain )
var o, d;
o = {
get foo() {
return 17; } };
d = Object.getOwnPropertyDescriptor(o, "foo");
// Return the following value
d {
configurable: true,
enumerable: true,
get: /*the getter function*/,
set: undefined
}
Object.getOwnPropertyNames()
Returns an array , It contains all enumerable or non enumerable property names of the specified object .( Include non enumerable properties but not Symbol Value as a property of name ) Array of components .
var arr = ["a", "b", "c"];
console.log(Object.getOwnPropertyNames(arr));
// ['0', '1', '2', 'length']
Object.getOwnPropertySymbols()
Method returns all of a given object itself Symbol Array of properties .
const dog = {
};
const r = Symbol("Roger");
const s = Symbol("Syd");
dog[r] = {
name: "Roger",
age: 6,
};
dog[s] = {
name: "Syd",
age: 5,
};
Object.getOwnPropertySymbols(dog); //[ Symbol(Roger), Symbol(Syd) ]
Object.getPrototypeOf()
Method returns the prototype of the specified object ( Inside [[Prototype]] The value of the property ).
function Obj() {
this.name = "ViavaCos";
}
Obj.prototype.fullName = " Nicholas · Zhao si ";
var obj = new Obj();
console.log(obj.__proto__.fullName); // Nicholas · Zhao si
console.log(Object.getPrototypeOf(obj) === obj.__proto__); // true Description address is consistent
Object.is()
Method to determine whether two values are the same value .
ES5 Compare two values for equality , There are only two operators : Equality operator () And strict equality operators (=). They all have shortcomings , The former will automatically convert data types , Latter NaN It's not equal to itself , as well as +0 be equal to -0.JavaScript Lack of an operation , In all circumstances , As long as the two values are the same , They should be equal .
ES6 Put forward “Same-value equality”( Equal in value ) Algorithm , To solve this problem .Object.is It's a new way to deploy this algorithm . It is used to compare whether two values are strictly equal , And strict comparison operators (===) The behavior is basically the same .
Object.is("foo", "foo");
// true
Object.is({
}, {
});
// false
// There are only two differences : One is +0 It's not equal to -0, Two is NaN Equal to itself .
//+0 === -0 //true
//NaN === NaN // false
Object.is(+0, -0); // false
Object.is(NaN, NaN); // true
//ES5 You can use the following code , Deploy Object.is.
Object.defineProperty(Object, "is", {
value: function (x, y) {
if (x === y) {
// in the light of +0 It's not equal to -0 The situation of
return x !== 0 || 1 / x === 1 / y;
}
// in the light of NaN The situation of
return x !== x && y !== y;
},
configurable: true,
enumerable: false,
writable: true,
});
Object.isExtensible()
Method to determine whether an object is extensible ( Can I add new properties to it )
// New objects are extensible by default .
var empty = {
};
Object.isExtensible(empty); // === true
// ... It can be made non scalable .
Object.preventExtensions(empty);
Object.isExtensible(empty); // === false
// Sealed objects are not extensible .
var sealed = Object.seal({
});
Object.isExtensible(sealed); // === false
// Frozen objects are also not extensible .
var frozen = Object.freeze({
});
Object.isExtensible(frozen); // === false
Object.isFrozen()
// An object is extensible by default , So it's also non frozen .
Object.isFrozen({
}); // === false
// A non extensible empty object is also a frozen object .
var vacuouslyFrozen = Object.preventExtensions({
});
Object.isFrozen(vacuouslyFrozen); //=== true;
// A non empty object is also non frozen by default .
var oneProp = {
p: 42 };
Object.isFrozen(oneProp); //=== false
Object.keys()
Method returns an array of enumerable properties of a given object , The property names in the array are arranged in the same order as those returned by normal loop traversal of the object .
// simple array
var arr = ["a", "b", "c"];
console.log(Object.keys(arr)); // console: ['0', '1', '2']
// array like object
var obj = {
0: "a", 1: "b", 2: "c" };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
Object.setPrototypeOf()
Method to set the prototype of a specified object ( namely , Inside [[Prototype]] attribute ) To another object or null.
Object.setPrototypeOf(obj, prototype)
Object.values()
Method returns an array of all enumerable property values of the given object itself , Order and use of values for…in The order of the cycles is the same ( The difference lies in for-in Loop through the properties in the prototype chain ).
var obj = {
foo: "bar", baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]
// array like object
var obj = {
0: "a", 1: "b", 2: "c" };
console.log(Object.values(obj)); // ['a', 'b', 'c']
Example method
hasOwnProperty()
Method returns a Boolean value , Indicates whether the specified property exists in the object's own property ( That is to say , Is there a specified key ).
const object1 = {
};
object1.property1 = 42;
console.log(object1.hasOwnProperty("property1"));
// expected output: true
console.log(object1.hasOwnProperty("toString"));
// expected output: false
isPrototypeOf()
Method is used to test whether an object exists in the prototype chain of another object .
function Foo() {
}
function Bar() {
}
function Baz() {
}
Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);
var baz = new Baz();
console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true
toLocaleString()
Method returns a string representation of the object . This method is used to derive objects for the purpose of a specific language environment (locale-specific purposes) And overload use .
obj.toLocaleString();
Object.prototype.valueOf()
valueOf() Method returns the original value of the specified object .
// Array: Returns the array object itself
var array = ["ABC", true, 12, -5];
console.log(array.valueOf() === array); // true
// Date: The current time span 1970 year 1 month 1 The number of milliseconds at midnight
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
console.log(date.valueOf()); // 1376838719230
// Number: Returns a numeric value
var num = 15.2654;
console.log(num.valueOf()); // 15.2654
// Boolean : Returns a Boolean value true or false
var bool = true;
console.log(bool.valueOf() === bool); // true
// new One Boolean object
var newBool = new Boolean(true);
// valueOf() The return is true, The two are equal
console.log(newBool.valueOf() == newBool); // true
// But not all , The two types are not equal , The former is boolean type , The latter is object type
console.log(newBool.valueOf() === newBool); // false
// Function: Return function itself
function foo() {
}
console.log(foo.valueOf() === foo); // true
var foo2 = new Function("x", "y", "return x + y;");
console.log(foo2.valueOf());
/* ƒ anonymous(x,y ) { return x + y; } */
// Object: Returns the object itself
var obj = {
name: " Zhang San ", age: 18 };
console.log(obj.valueOf() === obj); // true
// String: Return string value
var str = "http://www.xyz.com";
console.log(str.valueOf() === str); // true
// new A string object
var str2 = new String("http://www.xyz.com");
// The two are equal , But not all , Because of the different types , The former is string type , The latter is object type
console.log(str2.valueOf() === str2); // false
边栏推荐
- CGLIb 创建代理
- leetcode101. 对称二叉树
- Advanced C language: pointer (2)
- [database]
- There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
- 如何在VR全景中嵌入AI数字人功能?打造云端体验感
- Target detection notes fast r-cnn
- Win11快捷复制粘贴不能用怎么办?Win11快捷复制粘贴不能用
- High quality subroutine 3 - a good name
- PCA learning
猜你喜欢

【雷达】基于核聚类实现雷达信号在线分选附matlab代码

Programmer growth Chapter 30: artifact of identifying true and false needs

Date time functions commonly used in MySQL

Cglib create proxy

18 diagrams, intuitive understanding of neural networks, manifolds and topologies

参加竞赛同学们的留言 : 第十七届的记忆

A new paradigm of distributed deep learning programming: Global tensor

Target detection notes - overview and common data sets

RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块

程序员成长第三十篇:识别真伪需求的神器
随机推荐
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
Use FFT, matrix multiplication and conv2d to calculate convolution based on pytorch
Advanced C language: pointer (3)
Nacos配置热更新的4种方式、读取项目配置文件的多种方式,@value,@RefreshScope,@NacosConfigurationProperties
程序员成长第三十篇:识别真伪需求的神器
No code development platform management background tutorial
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
A simple neural network model based on MLP full connection layer
IOS interview
Vant web app installation reference
Programmer growth Chapter 30: artifact of identifying true and false needs
Elements in the middle (one article is enough)
Cglib create proxy
Thesis reading (0) - alexnet of classification
Typescript防止基类被实例化
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
Kotlin JVM annotation
Inheritance in swift
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
Several common methods of SQL optimization