search

js basic knowledge

Jun 08, 2020 pm 04:26 PM
javascript

Basic concepts of js

Local variables and global variables of js

js basic knowledge

js The data type

var is a weak data type, but js can recognize its data type

<head>
  <meta charset="utf-8">
  <title></title>
  <script type="text/javascript">
   function abc(){
    var a=1;
    var b="张三";
    var c=true;
    var d=new Date();
    
    alert("a的数据类型:"+typeof(a));
    alert("b的数据类型:"+typeof(b));
    alert("c的数据类型:"+typeof(c));
    alert("d的数据类型:"+typeof(d));
   }
  </script>
 </head>
 <body>
  <input type="button" name="" id="" value="js的数据类型" onclick="abc()"/>
 </body>

About js methods

Writing of methods

<script type="text/javascript">
  function test(){
   console.log("不传参数");
  }
  function test1(a){
   console.log("传1个参数"+a);
  }
  function test2(a,b){
   console.log("传2个参数:" +a+"第二个参数:"+b);
  }
  function abc(a){
   console.log("这是在abc的方法的值:"+a);
   return a;
  }
  function test3(a){
   var m=abc(a);
   console.log("调用了别人的返回值的方法"+m)
  }
  
 </script>
 <body>
  <input type="button" name="" id="" value="不传参数的按钮" onclick="test()" /><br />
  <input type="button" name="" id="" value="传1个参数" onclick="test1(12)" /><br />
  <input type="button" name="" id="" value="传2个参数" onclick="test2(1,&#39;张三&#39;)" /><br />
  <input type="button" name="" id="" value="调用了一个有返回值的按钮" onclick="test3(&#39;张三&#39;)" /><br />
 </body>

Method coverage

Unlike Java, there is no duplication of methods in js Load, only method override

as long as the method name is the same. No matter how many parameters there are, js will only recognize the last method (method override)

<head>
  <meta charset="utf-8">
  <title></title>
  
  <script type="text/javascript">
   function abc(a){
    //var name=&#39;张三&#39;;//在方法体内部的局部变量,只能自己用
    alert(&#39;这是第一个方法&#39;+a);
   }
   
   function abc(){
    alert(&#39;这是第二个方法&#39;);
   }
   
   function abc(){
    alert(&#39;这是真的&#39;);
   }
  </script>
 </head>
 
 <body>
  <!-- js中不会重载,只有方法覆盖啊 -->
  <input type="button"  value="方法重载和多态" onclick="abc(44)"/> 
  <!-- 输出:这是真的 -->
 </body>

js data type conversion

Although js only A var is used to describe a variable (weak data type), but the system can identify its data type and can also perform data type conversion

<script type="text/javascript">
  function test(){
   var x=&#39;12.3&#39;;
   console.log("x的数据类型是:"+typeof(x));
   var m=parseInt(x);
   console.log("x转换后的数据类型是:"+typeof(m)+"值是:"+m);
   
   var y=&#39;12.111&#39;;
   console.log("y的数据类型是:"+typeof(x));
   var m1=parseFloat(y);
   console.log("y转换后的数据类型是:"+typeof(m1)+"值是:"+m1);
   
   var z=&#39;3*4&#39;;
   console.log("z的数据类型是:"+typeof(z)+"z的值是:"+z);
   var m2=eval(z);
   console.log("z计算后的数据类型是:"+typeof(m2)+"值是:"+m2);
   
   var l=true;
   console.log("l的数据类型是:"+typeof(l)+"l的值是:"+l);
   var m3=l.toString();
   console.log("l转换后的数据类型是:"+typeof(m3)+"值是:"+m3);
  }
 </script>
 <body>
  <input type="button" name="" id="" value="数据类型的转换"  onclick="test()"/>
  </body>

Operation calculations in js

The operation rules of js are the same as Java (but special attention: x= y)

function abc(){
    var a=&#39;10&#39;;
    var b=&#39;8&#39;;
    console.log("b的值 "+b+"  b的数据类型转换成 "+typeof(b)+"  "+a)
    /* =+ a先转换成number 再给a的值复制给b */
    /* += 等价与 b+=a == b=b+a */
   }

js basic knowledge

Select statement and loop statement

Omitted: Same as java

js main object

window object

Time intervaler

<script type="text/javascript">
   function test(){  
    console.log(&#39;test方法开始执行了&#39;);
    /* 参数: 执行的方法, 等待的时间(毫秒单位) */
    window.setTimeout("hello()",1000);
   }
   function hello(){
    console.log(&#39;hello&#39;);
   }
   function test1(){
    console.log(&#39;test1方法开始执行了&#39;);
    window.setInterval("hello()",1000);
   }
  </script>
 </head>
 <body>
  <input type="button" value="等待一定时间,再执行" onclick="test()" /><br />
  <input type="button" name="" id="" value="每间隔一定时间,反复执行" onclick="test1()"/>
 </body>

js basic knowledge

Use of array

<script type="text/javascript">
  function test(){
   /* 第一种声明方式 */
   var a=[1,2,3,4,5,6,7,8,9];
   for (var i = 0; i < a.length; i++) {
    console.log("当前数组的角标:"+i+"当前的值:"+a[i]);
   }
  }
  function test1(){
   /* 第二种声明方式 */
   var a=new Array();
   a[0]=[1,2,3];
   a[1]=[&#39;张三&#39;,&#39;李四&#39;,&#39;王五&#39;];
   a[2]=[2,5,1,3,6];
   for (var i = 0; i < a.length; i++) {
    for (var j = 0; j < a[i].length; j++) {
     console.log("当前数组的角标:"+i+", "+j+"当前位置的值:"+a[i][j]);
    }
   }
  }
  function test2(){
   /* join(分隔符) 将数组元素中加分割符号后串接并返回一个字符串 */
   var a=[1,3,2,9,7,8,5];
   console.log(a.join("*"));
   /* reverse() 将数组元素按照原先相反位置存放 */
   console.log("数组的取反:"+a.reverse());
   /* slice(始[,终) 返回一个子数组  (前包后不包)*/
   console.log(a.slice(1,4));
   /* sort() 按照字母排序 */
   console.log(a.sort());
  }
 </script>
 <body>
  <input type="button"  value="一维数组的遍历" onclick="test()"/>
  <input type="button"  value="二维数组的遍历" onclick="test1()"/>
  <input type="button"  value="数组的操作" onclick="test2()"/>
 </body>

js basic knowledge

Basic operations on strings

<script type="text/javascript">
  function test(){
   var a="hello world";
   var index_a=a.indexOf("o");//第一个字母的位置
   var index_b=a.indexOf("p");//没有就返回-1
   console.log("o的角标位置:"+index_a);
   /* 字符截取(前包后不包) */
   var new_a=a.substring(1,3);
   console.log(new_a);
   /* 根据特定字符,格式化字符串 */
   var ip=&#39;192.168.0.1&#39;;
   var ip_array=ip.split(".");
   for (var i = 0; i < ip_array.length; i++) {
    console.log(ip_array[i]);
   }
   /* 大小写转换 */
   var b=&#39;abc&#39;;
   console.log(b.toUpperCase());
   var c=&#39;ABC&#39;;
   console.log(c.toLowerCase());
  }
 </script>
 <body>
  <input type="button" value="字符串处理" onclick="test()"/>
 </body>

js basic knowledge

Formatting of js time

<script src="../js/dateFormat.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
  function test(){
   var date=new Date();
   console.log(date);
   //方法1:引入控件
   var sdate=date.format(&#39;yyyy-MM-dd&#39;);
   var stime=date.format(&#39;yyyy-MM-dd HH:mm:ss&#39;)
   console.log(sdate);
   console.log(stime);
   
   //方法2:
   var y=date.getFullYear();//年
   var mon=date.getMonth()+1;//月
   var d=date.getDate();//日
   var h=date.getHours();//时
   var m=date.getMinutes();//分
   var s=date.getSeconds();//秒
   var weeks=date.getDay();
   var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
   console.log(y+"年 "+mon+"月 "+d+"日  "+h+":"+m+":"+s+"  "+weekday[weeks])
  }
 </script>
 <body>
  <input type="button" value="日期处理" onclick="test()" />
 </body>

js basic knowledge

Recommended tutorial: "JS Tutorial"

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

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

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.

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 Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

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 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.