当前位置:网站首页>JS基础4
JS基础4
2022-06-28 10:50:00 【程序员社区】
对象
定义
一组属性的无序集合,包含属性和方法,允许动态添加和删除属性
创建对象
1.构造函数是创建一个Object新实例,然后给它添加属性
let person = new Object();
person.name = 'Nicholas';
person.age = 29; //遍历输出 for(var key in person){
console.log(person[key])
}
//结果为Nicholas 29
- 字面量方式,直接花括号里添加属性
let person = {
name: 'Nicholas',
age: 29,
}
//遍历输出 for (var key in person) {
console.log(person[key])
}
//结果为Nicholas 29
对象内置特性
| 属性 | 含义 |
|---|---|
| Configurable | 是否可以通过delete删除并重新定义 |
| Enumberable | 是否可以通过for-in循环返回 |
| Writable | 属性的值是否可以被修改 |
| Value | 属性实际的值 |
要修改这些属性默认值true, 使用Object.defineProperty()方法,这个方法接受三个参数:给其添加属性的对象,属性的名称,一个描述对象
示例代码
let person = {
name: 'Nicholas',
age: 29,
}
Object.defineProperty(person, 'name', {
writable: true,
})
console.log(person.name)
//结果Nicholas
person.name = 'newNicholas';
console.log(person.name)
// writable: true结果Nicholas
// writable: false结果newNicholas
字符串对象
创建
- var str =new string(‘hello’)
- var str=‘hello’
字符串属性
| 属性 | 含义 |
|---|---|
| str[] | 访问字符串中字符 |
| charAt() | 找到索引号内容并返回 |
| indexof() | 找到字符串索引号 |
| lastIndexof() | 从后面找到字符串索引号 |
| substring() | 2个值,从哪个开始,到哪个结束,不包括结束字符 |
| substr() | 2个值,从哪开始,截取几个 |
| slice() | 2个值,从哪个开始,到哪个结束,不包括结束字符 |
| replace() | 用新字符串替换指定字符串 |
| split() | 分隔字符串,返回一个数组 |
| concat() | 字符串拼接 |
| trim() | 去除前后空格符 |
| startsWith(a) | 是否以a字符开头 |
| endWith(a) | 是否以a字符结尾 |
| includes(a) | a在全字符串是否存在 |
| toLowerCase | 转换小写 |
| toUpperCase | 转换大写 |
| repeat(N) | 将字符串复制N次 |
| padStart(n,‘m’) | 将m复制n次填充到字符串前面 |
| padEnd(n,‘m’) | 将m复制n次填充到字符串后面 |
注:
- 字符串中空格也是一个字符串
- indexof()找不到时返回-1
- slice,substring,substr
- 是以相同形式被调用,当只有一个数,提取到末尾位置
- 当有负数时,slice将所有负值当成字符串长度加上负参数值,substr将第一个负参数值当成字符串长度加上负参数值,第二个转换为0,substring将所有转换为0
示例代码
var str='hello';
console.log(str[0])
//结果h
var str='hello';
console.log(str.charAt(0))
//结果h
var str='hello';
console.log(str.indexOf('l'))
//结果02
var str='hello';
console.log(str.lastIndexOf('l'))
//结果03
var str='hello';
console.log(str.substring(0,2))
//结果he
var str='hello';
console.log(str.substr(0,2))
//结果he
var str='hello';
console.log(str.replace('he','eh'))
//结果ehllo
var str='hello';
newStr=str.split('')
console.log(newStr)
//结果['h', 'e', 'l', 'l', 'o']
var str='hello';
newStr=str.concat(' world')
console.log(newStr)
//结果hello world
var str=' hello';
newStr=str.concat(' world')
newStr1=newStr.trim()
console.log(newStr1)
//结果hello world
var str='hello';
newStr=str.startsWith('h')
console.log(newStr)
//结果true
var str='hello';
newStr=str.toUpperCase()
console.log(newStr)
//结果HELLO
for-in与for-of
- foe-in
var obj={
name:'Nichols',
age:19,
}
for(var key in obj){
console.log(obj[key])
//结果Nichols 19
}
- for-of
var str='hello'; for(var value of str){
console.log(value)
//结果h e l l 0
}
- for-in是得到键名(索引),for-of是得到元素值
- for-in可以遍历对象,数组
- for-of遍历数/数组对象/字符串/map/set等拥有迭代器对象的集合,但不能遍历对象
- for-in遍历原型链for-of遍历当前对象
模板字符串
定义:${变量}
- 反引号起来的字符串
- 模板语法,在字符串中输出变量的值
- 在模板语法中简单运算
边栏推荐
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- 物联网无线通信应用中6种融合定位技术
- Metersphere implements UI automation elements that are not clickable (partially occluded)
- How to use K-line diagram for technical analysis
- As shown in the figure, the SQL row is used to convert the original table of Figure 1. Figure 2 wants to convert it
- 关于FTP的协议了解
- 2022吴恩达机器学习specialization Week 2 practice lab: Linear regression
- JSON module, hashlib, Base64
- 物联网5种无线传输协议特点大汇总
- [Unity][ECS]学习笔记(一)
猜你喜欢

建立自己的网站(11)

sqlcmd 连接数据库报错

Ruoyi integrated building block report (NICE)

Katalon当中的debug调试

如何利用k线图做技术分析

Katalon框架测试web(二十)自定义关键字以及上传弹窗操作
![[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)](/img/02/4dbd97c8b8df1c96b7c8e1460aeda2.png)
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)

Remote connection of raspberry pie in VNC viewer mode without display

How to use dataant to monitor Apache apisex
![[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting](/img/a8/51b95432a9c8f25d8440cfd80926ce.png)
[NLP] this year's college entrance examination English AI score is 134. The research of Fudan Wuda alumni is interesting
随机推荐
Knowing the details of redis RDB, you can step on many holes less
物联网无线通信应用中6种融合定位技术
MySQL (I)
Redis database
Ruoyi integrated building block report (NICE)
[monkey] Introduction to monkey test
关于FTP的协议了解
Fabric.js 笔刷到底怎么用?
Spatial-Temporal时间序列预测建模方法汇总
知道 Redis RDB 这些细节,可以少踩很多坑
Markdown -- basic usage syntax
[200 opencv routines] 213 Draw circle
Google open source dependency injection framework Guice Guide
How to use dataant to monitor Apache apisex
AQS understanding
[Unity]EBUSY: resource busy or locked
无线通信模块定点传输-点对多点的具体传输应用
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
[Unity]EBUSY: resource busy or locked
How to use output in katalon