当前位置:网站首页>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
边栏推荐
- 程序员成长第三十篇:识别真伪需求的神器
- 【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码
- Summary of koltin knowledge points
- Empowering Chinese core entrepreneurs! See how Moore elite solves the development problems of small and medium-sized chip Enterprises
- Rouyi cloud platform - how to realize the launch and login functions of the project and how to create new modules
- 「行泊一体」放量,福瑞泰克高性能域控制器领跑新赛道
- Text is hidden beyond and ellipsis is displayed
- Performance optimized APK slimming
- Win11找不到DNS地址怎么办?Win11找不到DNS无法访问网页解决方法
- Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
猜你喜欢
![[image segmentation] vein segmentation based on directional valley detection with matlab code](/img/82/7b7b761c975cd5c2f5b8f3e43592d2.png)
[image segmentation] vein segmentation based on directional valley detection with matlab code

「行泊一体」放量,福瑞泰克高性能域控制器领跑新赛道

【滤波跟踪】基于EKF、时差和频差定位实现目标跟踪附matlab代码

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

安全狗入选《云安全全景图2.0》多个细项

6 open source tutorials of super conscience!

The safety dog has been selected into many details of cloud security panorama 2.0

Submission records of frontiers Publishing House (with status changes)

当初的“你“为什么做测试/开发程序员?自己存在的价值......

The functions and differences of display, visibility and overflow
随机推荐
MySQL Basics - Introduction and basic instructions
How strong is this glue?
RouYi-Cloud平台 ---项目的启动、登录功能是怎么实现的、怎么样创建新模块
Symbol symbol type
c语言进阶篇:指针(三)
MyCms 自媒体商城 v3.6 发布,兼容微擎应用开发(Laravel框架)
Programmer growth Chapter 30: artifact of identifying true and false needs
Sqlilabs-2 (breakthrough record)
frontiers出版社投稿记录(附状态变化)
Submission records of frontiers Publishing House (with status changes)
DirectX repair tool download (where is exagear simulator package)
18 diagrams, intuitive understanding of neural networks, manifolds and topologies
NVM use... Exit status garbled
The industry's first cloud native security detection dual model! Safety dog heavyweight report appears at the digital China Construction Summit
Cnpm installation steps
Mgr.exe virus caused the startup program to fail
【数据库】
终端输出g_debug()信息
[image segmentation] vein segmentation based on directional valley detection with matlab code
Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?