获取一个对象的所有key,返回值为数组.
它会遍历 所有的可枚举属性名 。
const objInfo = {
gzh: '测试',
author: 'HaiJun',
stack: 'Fond End'
}
console.log(Object.keys(objInfo)) //[ 'gzh', 'author', 'stack' ]
获取一个对象的 value , 返回值为数组
console.log(Object.values(objInfo)) //[ '测试', 'HaiJun', 'Fond End' ]
Object.assign() 方法将多个对象的属性进行合并,返回一个新对象。其中第一个参数是目标对象,在合并时,源对象的属性将覆盖目标对象中同名属性的值。
const objInfo = {
gzh: '测试',
author: 'HaiJun',
stack: 'Fond End'
}
const dataInfo = {
title: '前端从入门到放弃 😄'
}
console.log(Object.assign({},objInfo,dataInfo))
// {
// gzh: '测试',
// author: 'HaiJun',
// stack: 'Fond End',
// title: '前端从入门到放弃 😄'
// }
它是用来对目标对象的指定属性进行定义或修改。
它有三个必须参数:
descriptor 中可用的属性有:
一般情况下,我们改变的 descriptor 值不是所有参数都要修改的,所以该方法参数也支持简写形式,只指定要变化的属性即可,未指定则会使用默认值。
const personInfo = {}
Object.defineProperty(personInfo,'name',{value: 'HaiJun'})
console.log(personInfo)
//{name: 'HaiJun'}
返回一个指定值在字符串中首次出现的位置。如果没有找到匹配项,返回 -1。
const str = "hello world";
const pos = str.indexOf("world");
console.log(pos); // 6
返回一个指定值在字符串中最后一次出现的位置。如果没有找到匹配项,返回 -1。
const str = "hello world";
const pos = str.lastIndexOf("o");
console.log(pos); // 7
返回一个指定值在字符串中首次出现的位置。如果没有找到匹配项,返回 -1。与 indexOf() 的不同之处是,它可以接受正则表达式作为参数。
const str = "hello world";
const pos = str.search(/world/);
console.log(pos); // 6
****
****
在字符串中检索指定的值。如果找到一个或多个匹配项,则返回一个数组,其中包含该匹配项。
未找到匹配项,则返回 null。
与 search() 的不同之处是,它可以接受正则表达式作为参数,并且可以返回多个匹配项。
****
const str = "hello world";
const matches = str.match(/l/g);
console.log(matches); // ["l", "l", "l"]
在字符串中用一些字符替换另一些字符。如果有多个匹配项,只替换首个匹配项。可以使用正则表达式或字符串作为参数。
const str = "hello world";
const result = str.replace("world", "earth");
console.log(result); // "hello earth"
将字符串分割成字符串数组。可以使用正则表达式或字符串作为分隔符。
const str = "hello,world";
const arr = str.split(",");
console.log(arr); // ["hello", "world"]
window是浏览器的全局对象,常用属性如下:
****
****
在JavaScript中常用的保留小数的API如下:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。