当前位置:网站首页>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
边栏推荐
- Fabric. How to use js brush?
- 2D code generator for openharmony application development
- To enhance the function of jupyter notebook, here are four tips
- Interface automation framework scaffolding - Implementation of parametric tools
- Idea failed to connect to SQL Sever
- 无线模块透明传输技术的物联网应用案例
- mysql数据库概述以及安装过程
- metersphere使用js刷新当前页面
- [leetcode daily question] [December 19, 2021] 997 Find the town judge
- 乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
猜你喜欢

树莓派无需显示屏的VNC Viewer方式的远程连接

Metersphere实现UI自动化元素不可点击(部分遮挡)

Yann Lecun's new paper: the road to building automatic agents
![[monkey] Introduction to monkey test](/img/70/5a7152d0b6b77df7f9d6ad0e09e000.png)
[monkey] Introduction to monkey test

AGCO AI frontier promotion (6.28)

一种跳板机的实现思路

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

Katalon framework tests web (XX) custom keywords and upload pop-up operations
![[unity][ecs] learning notes (II)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[unity][ecs] learning notes (II)

How to use output in katalon
随机推荐
物联网5种无线传输协议特点大汇总
fastposter v2.8.4 发布 电商海报生成器
MySQL(一)
etf持仓如何影响现货金价?
Yann LeCun新论文:构建自动智能体之路
[Agora] get an Agora_ Example usage of refptr object
Who knows if it is safe to open an account with CSC securities
sentinel
JSON module, hashlib, Base64
使用 ABAP 操作 Excel 的几种方法
MySQL(三)
如何利用k线图做技术分析
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
Katalon framework tests web (XX) custom keywords and upload pop-up operations
Katalon当中的debug调试
Using loops for, while, and if in katalon else、break、continue
Fabric.js 笔刷到底怎么用?
[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)
[practice] appium settings app is not running after 5000ms
Ribbon核心源码解析