A word of nonsense before writing the content: Because some older browsers do not support javascript scripts, when encountering a script node, it is output as ordinary content. Therefore, sometimes in order to make the version compatible, comment symbols will be written on the content in the script node, like this In the old version, although the program will fail, our code will not appear, similar to this (please ignore the brackets and the things in the brackets):
Note: After testing, it was found that js statements cannot be written with comment symbols. On the same line, otherwise it will not work in new browsers.
Javascript data types:
Numeric types: including all numbers.
String type string is represented by "" or ''.
Boolean type boolean =true or false.
Variable: A container used to store data. The value stored in the program can be changed.
Declaration of variables: var variable name [= value];
If the variable is declared inside the function, then it is local. If it is outside the function, it is global. You know the meaning? of.
In other words, no matter what type of variable is declared, var is used. The specific data type is determined after initialization. If it is not initialized, it is a variable of undefined type.
Let’s use the small project mentioned in the previous js article to play with its numerical type.
Write in main.html:
Then we look at the output:
Here we can see that the reserved word typeof can return the name of the basic data type. In addition to these three, it can also return the type of object.
But if it is a special type, instanceof is needed to return the type name.
For example, if you define a var obj=new Object(); or var date=new Date(); and use typeof to return its type (typeof obj), the output is object
data Type conversion:
Convert from String to int/float using function: parseInt(String) / parseFloat(String) For example: parseFloat("3.14159");
Convert from numeric type to string , use the toString() function: for example:
var iNum=30; 30.toString(2);// Add a 2 in the following brackets, which means converting to a binary string.
//You can also have octal and hexadecimal. If there are no numbers, it will be converted directly.
The next step is to do a little experiment. Change the content under the script node in the file just now:
Then, run it~
Operations:
Operations are actually the same as those in C Java, they are all addition, subtraction, multipliers, and, or, There is actually no difference.
Let me reiterate the difference between i and i here.
i executes i 1 once, but returns i. For example, if I write a sentence
var i=50;
if(i document.write(i);
Then the runtime sequence is actually: assignment : i=50, judgment: if(iIf i is used there, the value returned is the value after adding 1, which means there will be no output.
-------------------------------------------------- ------------------------------------
Statement part
Conditional statements: if and switch
In fact, it is the same as c. This part does not need to be explained in detail. Even in the conditional statement, you should also pay attention to some small details:
In the if statement of js, false is returned in the following situations: null undefined empty character String "" 0 false
Also note about the empty string: var s="" and var s=new String("") are different. The latter opens up memory space, so it returns true.
Let’s verify it~, under the javascript node:
Then save and refresh the main page:
Swith statement also try:
View output:
The only difference between the first two is that the order of looping and judgment is different, do-while loops one more time than while , I won’t give an example.
I believe everyone is familiar with the for loop. Let’s look at the sentence for-in.
This is actually for arrays. The initialization of arrays in js is also quite strange. For example, we write it in the script node: (Also note the initialization of the array, using square brackets)
Let’s view the output:

From this result, we can explore a few points:
Among them, test is actually an int number to represent the number of the array.
for-in can only go through one number each time it loops, and is generally used for exhaustive enumeration.
In some cases, you can only use for-in for exhaustive enumeration. For example, the contents stored in the array include strings and numbers.
(Of course, if you insist on it, you don’t have to use for-in, but for-in is much more convenient)
Function is actually briefly mentioned in the first article.
1. There is no need to return a value before the function name, and there is no need to write a type in the parameter list.
2. Variables defined inside the function are local variables and cannot be called outside.
So the format is basically like this:
function function name (parameter list){
xxxxxxx;
[return xxxx;] //optional
}
OK, got it Now that we have the format, let’s try it:
The output is only numbers, dear, that is to say, the second line is ignored directly (what a tragedy), and it does not even meet the undefined standard. Because it is a local variable. It is discarded after the function is executed.
In addition to this standard writing method, there is also a rebellious way, that is, there are no parameters when defining a function, but parameters can also be used in the function body. In this way, when there are parameters, there can be output. This Because the parameters used have no names, they are all stored in the arguments array. For example:
PS: I changed the attributes in the script node , in fact, it means that javascript can also be declared like this. LANGUAGE must be capitalized
Look at the output:
Note that here, within the tested function, it is also possible to use the data stored in arguments to perform operations, such as
will output 48 4e55. Of course, because two parameters are used in the function body, if you only give one parameter when calling, the result will not output only one value. If you are interested in testing it yourself~ If you give 3 parameters when calling, Then the third parameter is ruthlessly ignored.
In addition to the above fairly regular definition, there are some other ways to define functions, which are relatively non-mainstream and I don’t like to use them, but I still have to write them down to understand:
One is: var add =new Function("parameter","parameter",...,"function body");
The output is correct. You can see that there is no need to add a semicolon in the last sentence. There is no problem. The reason here is that Function is actually a class, and then add becomes the name of the function.
There is another way to write:
Output everyone understands. . This way of writing is actually to write the function name first.
Now that it has been proven that the function is actually an object, of course it also has some functional functions that can be called, such as the toString() or valueOf() function that can be typed completely, and length can return the number of parameters of the function.
Let’s try it:
Output:
Hello Dumpling
function (name){ document.write("Hello " name); }
number of arguments: 1
OK That’s it for the second article ~ continue tomorrow (/^o^)/

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
