search
HomeWeb Front-endJS TutorialJavaScript study notes record my journey_Basic knowledge

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 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 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:
max = arr[i];
} }
return max;
}
var arr = new Array();
arr[0] = 20;
arr[1] = 10;
arr[2] = 34;
alert(MyMax(arr));



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:


Array() Exercise 3
(1) Output a string array in |-divided form, such as: Han Yinglong|Try|Order. Do not use the Join function in JavaScript. arr.join(1) joins the array into a string using delimiters.
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
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
Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment