Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the more practical JavaScript fragments?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the more practical JavaScript fragments, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. Ternary operator

Let someThingTrue = true if (someThingTrue) {handleTrue ()} else {handleFalse ()} * the following is the short version * let someThingTrue = true someThingTrue? HandleTrue (): handleFalse ()

two。 A short circuit or operation.

Const defaultValue = "SomeDefaultValue" let someValueNotSureOfItsExistance = null let expectingSomeValue = someValueNotSureOfItsExistance | | defaultValue console.log (expectingSomeValue) / / SomeDefaultValue

3. The condition is established

Let someValue = true if (someValue) {console.log ('conditional!')}

4. For cycle

For (let I = 0; I

< 1e2; i++) { // 代替 i pet.type === 'Dog' && pet.name === 'Tommy') console.log(pet) // { type: 'Dog', name: 'Tommy' } 12.默认参数值 早期的做法 function area(h, w) { if (!h) { h = 1; } if (!w) { w = 1; } return h * w } ES6 以后的做法 function area(h = 1, w = 1) { return h * w } 13.箭头函数的简写 let sayHello = (name) =>

{return `Hello, ${name} `} console.log (sayHello ('front-end intelligence'))

It is abbreviated as follows:

Let sayHello = name = > `Hello, ${name} `console.log (sayHello ('front-end intelligence'))

14. Implicit return

Let someFuncThatReturnSomeValue = (value) = > {return value + value} console.log (someFuncThatReturnSomeValue ('front-end intelligence'))

It is abbreviated as follows:

Let someFuncThatReturnSomeValue = (value) = > (value + value) console.log (someFuncThatReturnSomeValue)

15. Function must have parameter values

Function mustHavePatamMethod (param) {if (param = undefined) {throw new Error ('Hey You must Put some paramedics');} return param;}

To rewrite like this:

MustHaveCheck = () = > {throw new Error ('Missing parameterized')} methodShoudHaveParam = (param = mustHaveCheck ()) = > {return param}

Abbreviated 16.charAt ()

'SampleString'.charAt (0) / / S / / abbreviated 'SampleString' [0]

17. Conditional function call

Function fn1 () {console.log ('I am Function 1')} function fn2 () {console.log ('I am Function 2')} / * long writing * / let checkValue = 3; if (checkValue = 3) {fn1 ()} else {fn2 ()}

A short way to write:

(checkValue = 3? Fn1: fn2) ()

17.Math.Floor abbreviation

Let val = '123.95' console.log (Math.floor (val)) / / conventional console.log (~ val) / / abbreviated

18.Math.pow abbreviation

Math.pow (2,3) / / 8 / / abbreviated 2 * * 3 / / 8

19. Convert a string to a number

Const num1 = parseInt ('100') / / console.log (+ "100') console.log (+" 100.2 ")

20.calculate & Operation

Let value = 1; if (value = 1) console.log ('Value is one') / / OR In short value & & console.log (' Value is one')

21.toString abbreviation

Let someNumber = 123console.log (someNumber.toString ()) / / "123" / / abbreviated console.log (`$ {someNumber} `) / /" 123"

twenty-two。 Optional chain operator (coming soon)

Now there is a new proposal for ECMAScript, which is worth knowing.

Let someUser = {name: 'Jack'} let zip = someUser?.address?.zip / / optional link, like Swift

If zip is undefined, no error is raised.

The syntax also supports function and constructor calls

Let address = getAddressByZip.? (12345)

If getAddressByZip is the function that calls it, otherwise, the expression will be evaluated as undefined.

23. Replace switch syntax with an object

Let fruit = 'banana'; let drink; switch (fruit) {case' banana': drink = 'banana juice'; break; case' papaya': drink = 'papaya juice'; break; default: drink =' Unknown juiceholders'} console.log (drink) / / banana juice is helpful to you after reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report