Assign initial values to parameters There is no repetition in JavaScript The concept of loading, but the parameters of functions in JavaScript are optional. If one parameter is missing when calling, it will be replaced by undefined.
function plus(base, added) { return base added; } plus(2); // NaN
In this example, plus(2) and plus(2, undefined) are equivalent, and the result of 2 undefined is NaN. The question now is, if the second parameter is not passed, how to assign an initial value to it?
function plus(base, added) { added = added || (added === 0 ? 0 : 1); return base added; }
Prevent others from loading your page in an Iframe If your When a website becomes very popular, many websites will want to link to your website, and even want to embed your web page into its own web page through IFrame. This is not fun, so how to stop this behavior?
This code should be placed in the head of each of your pages. If you want to know if anyone is using it in real life, just take a look at Baidu's blog and you will know. The
String replacement String.prototype.replace function often confuses programmers who are very familiar with C# or Java. For example:
'Hello world, hello world'.replace('world' , 'JavaScript'); // The result is "Hello JavaScript, hello world"
replaceThe first parameter of the function is a regular expression. If you pass a string as the first parameter, only the first matching string found is replaced. To solve this problem, we can use regular expressions:
'Hello world, hello world'.replace(/hello/gi, 'Hi'); // The result is "Hi world, Hi world"
Convert arguments into an array The predefined variable arguments in the function is not a real array, but an array-like object. It has the length attribute, but it does not have functions such as slice, push, sort, etc. So how to make arguments have functions that are only available for these arrays? In other words, how to make arguments into a real array?
The second parameter is optional and is used to specify the decimal number of the first parameter. If the second parameter is not passed, the following rules are followed: -> If str starts with 0x, it is considered to be hexadecimal. ->If str starts with 0, it is considered to be octal. -> Otherwise, it is considered to be decimal. So the following code will be confusing if you don’t know the rules:
As you can see, delete cannot actually delete an element in the array. The deleted elements will be replaced by undefined, and the length of the array will not change.
In fact, we can delete elements in the array through the splice function in Array.prototype, as shown below:
Add console.log function for IE Under Firefox and with the support of Firebug, we often use console.log to record some information in the console. However, this approach will prevent the execution of JavaScript under IE (the same is true when Firebug is not enabled under Firefox), because there is no console object at all at this time. We can use the following tips to prevent this situation from happening:
This code may seem strange to you, but it does work. undefined is just JavaScript Just a predefined variable. Note: Never do this in a JavaScript program, this trick just tells you that this is the case.
Determine whether a variable is undefined In two cases, a variable is undefined: 1. The variable is declared, but no value is assigned
name2 === undefined; // error – name2 is not defined
In the second case, an error will be thrown, so what if you determine whether a variable is undefined without generating an error? A general method is provided below:
Preload images Preload images is to load images that do not exist on the page so that they can be quickly displayed later using JavaScript . For example, if you want to display another picture when the mouse moves over a picture:
var source = ['img1.gif','img2.gif ']; var img = new Image(); for(var i = 0; i < source.length; i ) { img.src = source[i]; }
In fact, this code can only Load the last image, because the other images don't have time to preload when the loop comes. So the correct way to write it should be:
var source = ['img1.gif ','img2.gif']; for(var i = 0; i < source.length; i ) { var img = new Image(); img.src = source[i]; }
Closure Closure refers to a local variable within a function, which is still available when the function returns. When you define another function inside a function, you create a closure, a famous example:
(function (){ for(var i = 0; i < 2; i ) { } })(); typeof(i) === 'undefined'; // true
Weird NaN NaN is used to indicate that a value is not a number. NaN behaves strangely in JavaScript because NaN is not equal to any value (including itself).
True and false values All values in JavaScript can be implicitly converted to Boolean type. In conditional judgment, the following values will be automatically converted to false: null, undefined, NaN, 0, '', false Therefore, there is no need to make the following complicated judgments :
new Boolean(false) === false; // false new Boolean(false) == false; // true typeof(new Boolean(false)); // " object" typeof(Boolean(false)); // "boolean"
Fast string concatenation We often use to convert shorter Strings are concatenated into one long string, which is fine in most cases. But if there are a large number of strings that need to be concatenated, this approach will encounter performance problems, especially under IE.
var startTime = new Date(); var arr = []; for (var i = 0; i < 100000; i ) { arr.push(i); } var str = arr.join(""); alert(new Date() - startTime); // Firefox - 38ms, IE7 - 280ms
You can see Firefox It seems that the operator has been optimized, while IE behaves stupidly.
Unary operator
In JavaScript, we can use the unary operator " " before a string. This will convert the string to a number, returning NaN if the conversion fails.
If used in front of a non-string, conversion attempts will be made in the following order:
Call valueOf() and call toString() to convert it into a number
encodeURI and encodeURIComponent window.encodeURI function is used to encode a URL, but the following characters will not be encoded: ":", "/", " ;", "?". window.encodeURIComponent will encode the above characters. We illustrate with an example:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn