js miscellaneous notes

不言
Release: 2018-04-26 14:34:16
Original
1066 people have browsed it

This article introduces you to some basic content in js. Friends who are interested can take a look at

1.
Undefined This value means that the variable does not contain a value.


You can clear the variable by setting the value of the variable to null
2.
You can access the object properties in two ways:
1.person.lastName;
2.person["lastName"];
3.
If you assign a value to a variable that has not been declared, the variable will be automatically declared as a global variable.


This statement:


carname="Volvo";
will declare a global variable carname even if it is executed within a function.
At the same time, it is necessary to call this global variable outside the function so that the function can be executed, otherwise the global variable will not be created!
4.
let allows you to declare a variable, statement or expression whose scope is limited to the block level. Different from the var keyword, the variables it declares can only be global or the entire function block.

function varTest() { var x = 1; if (true) { var x = 2; // 同样的变量! console.log(x); // 2 } console.log(x); // 2 } function letTest() { let x = 1; if (true) { let x = 2; // 不同的变量 console.log(x); // 2 } console.log(x); // 1 }
Copy after login

5.
The default execution comparison of the switch statement is ===
6.
Strict mode statement
Strict mode is passed at the head of the script or function Add "use strict"; expression to declare.
7.
Precautions for using floating point data
All data in JavaScript is stored as 64-bit floating point data (float).


All programming languages, including JavaScript, are difficult to determine the accuracy of floating-point data:


var x = 0.1; var y = 0.2; var z = x + y // z 的结果为 0.3 if (z == 0.3) // 返回 false
Copy after login

To solve the above problems , can be solved by multiplication and division of integers:
var z = (x * 10 + y * 10) / 10; // The result of z is 0.3
eight,
Use the return value directly in the string An error will be reported when changing the line:
var x = "Hello
World!";
//Error report
The solution is to add escape characters\
9.
void() is just Yes means that no value is returned, but the expression in the brackets still needs to be run, such as void(alert("Wornning!"))
10.
Function expressions can be "self-calling".


Self-calling expressions will be called automatically.


If the expression is followed by (), it will be called automatically.


The declared function cannot be called by itself.

(function () { var x = "Hello!!"; // 我将调用自己 })(); JavaScript 函数作为一个值使用: JavaScript 函数可作为表达式使用:
Copy after login

Functions are defined as properties of objects, called object methods.
If the function is used to create a new object, it is called the constructor of the object.
11.
The above functions do not belong to any object. But in JavaScript it is always the default global object.


The default global object in HTML is the HTML page itself, so the function belongs to the HTML page.


The page object in the browser is the browser window (window object). The above functions will automatically become functions of the window object.


myFunction() and window.myFunction() are the same:
Twelve,
this is a keyword in the JavaScript language.


It represents an internal object automatically generated when the function is running and can only be used inside the function. For example:

function test() { this.x = 1;
Copy after login

}
The value of this will change as the function is used. But there is a general principle, that is, this refers to the object that calls the function.
Thirteen,
Global variables.


Global variables in web pages belong to the window object.


Global variables apply to all scripts on the page.
14.
In JavaScript, this usually points to the function we are executing, or to the object to which the function belongs (runtime)
15.
JavaScript arrays only support Numeric index, non-numeric index refers to the properties of the object.
JavaScript arrays can be regarded as special objects


If the subscript value is within the legal range, it will be the same regardless of whether the subscript value is a number or a numeric string. will be converted into numbers for use, that is, array["100"] = 0 and array[100] = 0 perform the same operation.

Related recommendations:

js-notes

JS implements JAVA’s List function

The above is the detailed content of js miscellaneous notes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!