当前位置:网站首页>JS foundation 4
JS foundation 4
2022-06-28 10:53:00 【Programmer community】
object
Definition
An unordered collection of attributes , contain attribute and Method , Allow dynamic addition and deletion of attributes
Create objects
1. The constructor is to create a Object New examples , Then add attributes to it
let person = new Object();
person.name = 'Nicholas';
person.age = 29; // Traverse the output for(var key in person){
console.log(person[key])
}
// The result is Nicholas 29
- Literal measure , Add attributes directly to curly braces
let person = {
name: 'Nicholas',
age: 29,
}
// Traverse the output for (var key in person) {
console.log(person[key])
}
// The result is Nicholas 29
Object built-in properties
| attribute | meaning |
|---|---|
| Configurable | Is it ok to pass delete Delete and redefine |
| Enumberable | Is it ok to pass for-in Loop back |
| Writable | Whether the value of the attribute can be modified |
| Value | The actual value of the property |
To modify the default values of these properties true, Use Object.defineProperty() Method , This method takes three parameters : The object to which the attribute is added , Name of property , A description object
Sample code
let person = {
name: 'Nicholas',
age: 29,
}
Object.defineProperty(person, 'name', {
writable: true,
})
console.log(person.name)
// result Nicholas
person.name = 'newNicholas';
console.log(person.name)
// writable: true result Nicholas
// writable: false result newNicholas
String object
establish
- var str =new string(‘hello’)
- var str=‘hello’
String properties
| attribute | meaning |
|---|---|
| str[] | Access the characters in the string |
| charAt() | Find the index number contents and return |
| indexof() | Find the string index number |
| lastIndexof() | Find the string index number from the back |
| substring() | 2 It's worth , Where to start , To which end , The ending character is not included |
| substr() | 2 It's worth , Where to start , Intercept a few |
| slice() | 2 It's worth , Where to start , To which end , The ending character is not included |
| replace() | Replace the specified string with a new string |
| split() | Delimited string , Returns an array |
| concat() | String splicing |
| trim() | Remove the leading and trailing space characters |
| startsWith(a) | Whether or not to a The beginning of a character |
| endWith(a) | Whether or not to a Character ending |
| includes(a) | a Whether the full string exists |
| toLowerCase | Convert lowercase |
| toUpperCase | Convert a capital |
| repeat(N) | Copy the string N Time |
| padStart(n,‘m’) | take m Copy n Times to fill in front of the string |
| padEnd(n,‘m’) | take m Copy n Fill the string twice |
notes :
- A space in a string is also a string
- indexof() Return when you can't find it -1
- slice,substring,substr
- Is called in the same form , When there is only one number , Extract to end position
- When there is a negative number ,slice Treat all negative values as string lengths plus negative parameter values ,substr Take the first negative parameter value as the string length plus the negative parameter value , The second conversion is 0,substring Convert all to 0
Sample code
var str='hello';
console.log(str[0])
// result h
var str='hello';
console.log(str.charAt(0))
// result h
var str='hello';
console.log(str.indexOf('l'))
// result 02
var str='hello';
console.log(str.lastIndexOf('l'))
// result 03
var str='hello';
console.log(str.substring(0,2))
// result he
var str='hello';
console.log(str.substr(0,2))
// result he
var str='hello';
console.log(str.replace('he','eh'))
// result ehllo
var str='hello';
newStr=str.split('')
console.log(newStr)
// result ['h', 'e', 'l', 'l', 'o']
var str='hello';
newStr=str.concat(' world')
console.log(newStr)
// result hello world
var str=' hello';
newStr=str.concat(' world')
newStr1=newStr.trim()
console.log(newStr1)
// result hello world
var str='hello';
newStr=str.startsWith('h')
console.log(newStr)
// result true
var str='hello';
newStr=str.toUpperCase()
console.log(newStr)
// result HELLO
for-in And for-of
- foe-in
var obj={
name:'Nichols',
age:19,
}
for(var key in obj){
console.log(obj[key])
// result Nichols 19
}
- for-of
var str='hello'; for(var value of str){
console.log(value)
// result h e l l 0
}
- for-in Yes, get the key name ( Indexes ),for-of Is to get the element value
- for-in You can traverse objects , Array
- for-of Chronology / Array objects / character string /map/set Such as a collection of iterator objects , But can't traverse objects
- for-in Traverse the prototype chain for-of Traverse the current object
Template string
Definition :${ Variable }
- A string enclosed in inverted quotation marks
- Template syntax , Output the value of a variable in a string
- Simple operations in template syntax
边栏推荐
- Katalon全局变量在TestObject引用
- AQS understanding
- Katalon global variable is referenced in testobject
- BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
- Realization of a springboard machine
- 数据库系列:有什么办法对数据库的业务表进行无缝升级
- Ribbon core source code analysis
- File的io流与base64
- Katalon framework tests a web page operation example code
- Redis database
猜你喜欢

MySQL(一)

一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!

Wireless communication module fixed-point transmission - point to multipoint specific transmission application

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

第六天 脚本与动画系统

【monkey】monkey测试入门

Mysql通用二进制安装方式
![[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)](/img/e8/21d8d81a3d7b544687d6adc06ad4b1.png)
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)

一种跳板机的实现思路

阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
随机推荐
MySQL general binary installation method
[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)
线程和线程池
Knowing the details of redis RDB, you can step on many holes less
An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
Katalon全局变量在TestObject引用
Katalon global variable is referenced in testobject
Hystrix deployment
Information hidden in the trend chart of Hong Kong London gold market
一款自动生成单元测试的 IDEA 插件,开发效率提升 70% 以上!
方法重写(Override)
[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)
2022吴恩达机器学习specialization Week 2 practice lab: Linear regression
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
MarkDown——基本使用语法
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
appliedzkp zkevm(9)中的Bytecode Proof
【monkey】monkey测试入门
第六天 脚本与动画系统
Summary of spatial temporal time series prediction modeling methods