Home Web Front-end JS Tutorial JavaScript study notes record my journey_Basic knowledge

JavaScript study notes record my journey_Basic knowledge

May 16, 2016 pm 05:53 PM
study notes

1. What is JavaScript?
(1) HTML is just a markup language that describes the appearance of a web page. It does not have the ability to calculate or judge. If all calculations and judgments are made (such as judging whether the text box is empty, judging whether two passwords have been entered) Consistent) If the store executes it on the server side, the web page will be very slow, difficult to use, and put a lot of pressure on the server. Therefore, it is required to perform some simple operations in the browser. To judge, JavaScript is a A language that executes on the browser side.
(2) JavaScript and Java have no direct relationship. The only relationship is that JavaScript was originally called LiveScript. Later, it absorbed some features of Java and was upgraded to JavaScript. JavaScript is sometimes referred to as JS.
(3) JavaScript is an interpreted language and can be executed at any time without compilation. In this way, even if there are grammatical errors, the parts without grammatical errors can still be executed correctly.
JS development environment
(1) JavaScript and Jqery auto-complete function in VS.
(2) JS is a very flexible dynamic language, not as rigorous as static languages ​​such as C#.
JS Getting Started
(1)

Copy code The code is as follows:

< ;script type="text/javascript">
alert(new Date().toLocaleDateString());


(2) JavaScript code is placed in In the <script> tag, <script> can be placed at any position such as <head>, <body>, and there can be more than one <script> tag. The alert function is a pop-up message window, and new Date() is to create an object of Date class, and the default value is the current time. <br>(3) The <script> placed in <head> has been run before the body is loaded, and the <script> written in the body is executed one by one as the page is loaded. <br>(4) In addition to declaring JavaScript in the page, you can also write JavaScript in a separate JS file and then introduce it into the page: <script src=”common.js” type=”text/javascript ”></script>. The advantage of declaring to a separate JS file is that multiple pages can be shared, reducing network traffic.
Event
(1) Click me
I won’t pop up anything

Click me
(2) JavaScript also has the concept of events, when the button is clicked
1)
2) Only the JavaScript in the href of the hyperlink requires "JavaScript:", because it is not an event, but treats "JavaScript:' like" http:", "ftp:", "thunder://", ed2k://, mailto:// are the same network protocols and are processed by the JS parsing engine. There is only this special column in href.
JS variable
(1) You can use double quotes or single quotes to declare strings in JavaScript, mainly to facilitate integration with HTML and avoid the trouble of escape characters.
(2) var i=10. ; //Declare a variable with the name i, pointing to the integer 10. Once it points to 10, i is of type int, alert(i);
(3) There are two types of null and underfined in JavaScript, and null represents the value of the variable. If it is empty, underfined means that the variable does not point to any object and has not been initialized.
(4) JavaScript is a weak type and cannot represent variables: int i=10. Variables can only be declared through var i=10; Unlike var in C#, it is not type inference like in C#
(5) In JavaScript, you can also use var directly instead of declaring variables. Such variables are "global variables", so unless you really want to use them globally. Variable, otherwise it is best to add var when using it.
(6) JS is dynamically typed, so var i=10;i="abc" is legal.
JavaScript
(1)
Copy code The code is as follows:

var sum = 0;
for (var i = 0; i <= 100; i ) {
sum = sum i;
}
alert(sum);

(2) If the code in JavaScript has syntax If an error occurs, the browser will pop up an error message. Checking the error message can help troubleshoot the error.
(3) JavaScript debugging, using VS can easily debug JavaScript. You need to pay attention to a few points when debugging:
1) IE debugging options should be turned on, Internet Options-Advanced, remove "Disable script debugging" Check the box in front of ".
2) Run the interface in debug mode.
3) Setting breakpoints, monitoring variables and other operations are the same as C#.
Judge variable initialization
(1) Three ways to judge whether variables and parameters are initialized in JavaScript.
Copy code The code is as follows:

var r;
if (r == null) { if (typeof (r) == "undefined") { if (!x) {
alert("null"); alert ("undefined"); alert("Not 🎜>(1) Method of declaring functions in JavaScript:



Copy code


The code is as follows:
function Add(i1, i2) { return i1 i2; } (2) There is no need to declare the return value type, parameter type, and the function definition starts with function



Copy code

The code is as follows:
(3) JavaScript does not require all All paths have return values.
Anonymous function
(1)



Copy code


The code is as follows:
var f1 = function sum(i1, i2) { return i1 i2; } alert(f1(10, 20));
(2) similar to C# anonymous function.
(3) This kind of anonymous usage is particularly common in Jquery.
(4)



Copy code


The code is as follows:
alert(function sum(i1 , i2) { return i1 i2; } (100, 10)); Note: Anonymous functions in C# are called using delegates.
JS object-oriented basics
(1) There is no class syntax in JavaScript, which is simulated by function closure. When explaining below, we still use concepts such as classes and constructors in C#. In JavaScript "Classes" such as string and date are called "objects", and classes are declared in JavaScript (classes are not classes, but objects).
(2)



Copy code


The code is as follows:
function Person(name, age ) { //Declare a function as a class using this.Name = name; this.Age = age; this.SayHello = function () { alert("Hello, I It is " this.Name ", my year is: " this.Age "year old"); }
}
var p1 = new Person("Han Yinglong", "23");
p1 .SayHello();


(3) The class name must be declared. function Person(name,age) can be regarded as declaring the constructor. Name and Age attributes are also dynamically added by the user.
Array() object
(1) The Array object in JavaScript is an array. First of all, it is a dynamic array, and it is a super complex like arrays ArrayList, Hashtable, etc. in C#.
(2)



Copy code


The code is as follows:
var names = new Array( ); names[0] = "Han Yinglong"; names[1] = "get"; names[2] = "said"; for (var i = 0; i < names.length; i ) { alert(names[i]);
}


(3) No need to pre-set the size, dynamic.
Array() Exercise 1
(1) Array exercise, find the maximum value in an array.



Copy code


The code is as follows:



Array( ) Exercise 2
(1) Reverse the order of the elements of a string array, {3,9,5,34,54}{54,34.5.9.3}. Do not use the reverse function in JavaScript. Tip: Swap the i-th and length-i-1 to define the function.




Copy code


The code is as follows:



Dictionary usage of Array
(1 ) Array in JS is a treasure, not only an array, but also a Dictionary and a Stack.
(2)
Copy code The code is as follows:

var names = new Array( );
names["人"] = "ren";
names[" buckle"] = "kou";
names["hand"] = "shou";
alert(names ["人"]);
alert(names.人);
for (var k in names) {
alert(k);
}

( 3) Use it like Hashtable and Dictionary, and it is as efficient as them.
Simplified declaration of Array()
(1) Array can also have a simplified way
var arr=[3,4,5,6,7]; //Ordinary array initialization
This Arrays can be seen as a special case of names["人"]="ren";, that is, the keys are 0,1,2,3,4,5
(2) Simplified creation method in dictionary style
var arr={”tom”=30,”jim=”30};
Array, for and others
(1) For array-style Array, you can use the join method to concatenate it into a string.
Copy code The code is as follows:

var arr = ["tom", "jim", "kencery"];
alert(arr.join(",")); //join in JS is an array method, unlike .net which is a string method

(2 ) for loop can be used like foreach in C#.
Copy code The code is as follows:

for (var e in document) {
alert (e);
}
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles