Summary of js daily knowledge points

Summary of JS daily knowledge points


foreword

This article mainly summarizes the commonly used and necessary knowledge points of js.


1. The method of dynamically passing parameters in js function

1. In some scenarios, it is necessary to encapsulate a public method and implement public processing inside the method.
For example: There are many places in the page that need to clear the timer.
Encapsulation idea: By passing the corresponding parameter identifier, the timer corresponding to the parameter identifier can be cleared inside the method.

clearTimer(...clearType) {
    
    
	for (let i= 0; i < clearType.length; i++) {
    
    
		if (clearType[i] === 'timer1') {
    
    
			clearInterval(this.orderTimer)
			this.orderTimer = null
		} else if (clearType[i] === 'timer2') {
    
    
			clearInterval(this.timerResult)
			this.timerResult = null
		} else {
    
    
			clearInterval(this.timer)
			this.timer = null
		}
	}
}
clearTime('timer1','timer2')

Passing parameters through destructuring can pass multiple parameters at the same time. Multiple parameters passed to the function will be saved in the form of an array. Through traversal, the corresponding timer is cleared. The parameters of wearing at the same time are not limited by the fixed position.

Two, JS dynamic parameter transfer method 2

1. Use the arguments object inside the js function body to receive the incoming calls, and use this object property to pass parameters.

function he() {
    
    
    let sum = 0;
    for(let i = 0;i < arguments.length; i++) {
    
    
        sum = sum + arguments[i];
    }
    return sum;
}
he(1,2,3);

Here you can see that although arguments is an object type, it can get the corresponding element by array index and traverse

3. How to judge whether an object contains a certain attribute

1. The most basic method is to use the most original characteristics of the object:
if there is no attribute in the object, it will return undefined.
You can judge whether there is an attribute by directly taking the value.

let obj = {
    
    a:1,b:2}
obj.c    //返回undefined
但是需要注意的是,如果key对应的value就是undefined的话那么返回的结果也就是undefined

2. Through in


let obj = {
    
    a:1};
'a' in obj;             // true
'b' in obj;             // false

3. The most direct method is to use the method provided by object, hasOwnProperty()

let obj = {
    
    a:1,b:2};
obj.hasOwnProperty('a');  

Fourth, the method of decoding and encoding URI in js

1. Definition and usage
The decodeURIComponent() function can decode the URI encoded by the encodeURIComponent() function.
A URI can be encoded using the encodeURIComponent() method.

2. The parameter
uri is required. A string containing encoded URI components or other text to decode.
3. For example:

let test1="http://www.baidu.com/123/"
document.write(encodeURIComponent(test1))
document.write(decodeURIComponent(test1))
输出:
http%3A%2F%2Fwww.baidu.com%2F123%2F
http://www.baidu.com/123/

5. String coercion conversion pit

When using !!""the conversion string, it will not be converted to sum like numberthe type , but will be converted to true as long as the string is not empty.10truefalse

six,. . . Continually updated

Summarize

If there is a problem, please point it out in time, and you must humbly accept the correction. . . learn from each other

Guess you like

Origin blog.csdn.net/qq_43205326/article/details/118372545