search
HomeWeb Front-endJS TutorialSummary of JavaScript learning (1) - basics of getting started with JavaScript_javascript skills

1. JavaScript language features

1.1. JavaScript is object- and event-driven (dynamic)

It can respond directly to user or customer input without going through the Web service program. It responds to users in an event-driven manner. The so-called event-driven refers to the action generated by performing a certain operation on the homepage, which is called an "event". For example, pressing the mouse, moving the window, selecting the menu, etc. can be regarded as events. When an event occurs, a corresponding event response may be triggered.

1.2. JavaScript is cross-platform

JavaScript is dependent on the browser itself and has nothing to do with the operating system.

2. JavaScript variables

2.1. Define variables

When defining variables, always use "var variable name", for example: var str; you can even omit the keyword var

2.2. How to determine the type of JavaScript variables

The data type of variables in JavaScript is determined by the JS engine

 var name="孤傲苍狼";//name是string类型
 var age=;//age是number类型
 var flag=true;//flag是boolean类型
 var email;//email只是声明,没有赋值,因此代表的类型是"undefined",也就是无法确定
 name=;//name自动变成了number类型

2.3. Use the typeof keyword to view the specific data type represented by the variable

The typeof operator has one parameter, which is the variable or value to be checked. For example:

 var sTemp = "test string";
 alert (typeof sTemp);  //输出 "string"
 alert (typeof );  //输出 "number"

Calling the typeof operator on a variable or value will return one of the following values:

undefined - if the variable is of type Undefined

boolean - if the variable is of type Boolean

number - if the variable is of type Number

string - if the variable is of type String

object - if the variable is a reference type or of type Null

Test code:

<script type="text/javascript">
   var name="孤傲苍狼";//name是string类型
   alert("name是"+typeof name+"类型");
   var age=;//age是number类型
   alert("age是"+typeof age+"类型");
   var flag=true;//flag是boolean类型
   alert("flag的类型是:"+typeof flag);
   name=;//name自动变成了number类型
   alert("name变量重新赋值后,name的数据类型变成了:"+typeof name);
   var email;//email只是声明,没有赋值,因此代表的类型是"undefined",也就是无法确定
   alert("email的类型是:"+typeof email );
   var a=null;
 /*
 为什么 typeof 运算符对于 null 值会返回 "Object"。
 这实际上是 JavaScript 最初实现中的一个错误,然后被 ECMAScript 沿用了。现在,null 被认为是对象的占位符,从而解释了这一矛盾,但从技术上来说,它仍然是原始值。
 */
   alert("a是"+typeof a +"类型");
  </script>

Run result:

3. JavaScript data types

 

JavaScript contains two different data types: basic data types and reference data types. Primitive types refer to simple data, and reference types refer to objects composed of multiple values. When we assign a value to a variable, the first thing the parser has to do is to confirm whether the value is a primitive type value or a reference type value.

3.1. Basic data types

 Five common basic data types:

Boolean
Number
String
Undifined
Null 

These five basic data types can directly operate on the actual values ​​stored in variables.

3.1.1, Numeric type (Number) and Boolean type (Boolean)

Look at the code below:

 <script type="text/javascript">
   var a = ;
   var b = a;
     b = ;
   alert("a="+a);//打印a=
   
   var b = true;
   var b = b;
     b = false;
   alert("b="+b);//打印b=true
 </script>

Running results:

It can be seen from the running results that the value of b is a copy of the value of a. Although the values ​​of the two variables are equal, the two variables store two different basic data type values. b just saves a copy of a's copy. So, when the value of b changes to 20, the value of a is still 10. The two Boolean variables b1 and b2 are also basic data types and also store two different basic data type values. b2 stores a copy of 1. Therefore, when the value of b2 changes to false, the value of b1 is still true.

The following figure demonstrates the process of assignment of this basic data type:

Stack memory

3.1.2. String type (String)

String in JavaScript is a special basic data type. In many languages, String is represented in the form of an object, but in JavaScript, String is treated as a basic data type and is passed by value. way to operate. But it is a rather special basic type.

Look at the example below:

<script type="text/javascript">
   var strA = "这是字符串";
   var strB = strA;
     strA = "这是另外一个字符串";
     alert("strB="+strB);
 </script>

运行结果:

  

  从运行结果可以看到,仿佛strA通过值传递的方式复制了一份给了strB。当strA改变的时候,strB并没有改变,似乎我们已经可以下结论,String就是个基本数据类型。

可是,因为String是可以任意长度的,通过值传递,一个一个的复制字节显示效率依然很低,看起来String也可以当作引用类型。

看下面例子:

 var a = "myobject";
 a.name = "myname";//为字符串a动态添加name属性
 alert("a.name="+a.name); //访问a的name属性,

结果显示:a.name=undefined

运行结果:

  

  运行结果显示,String无法当作一个对象来处理。这也证明了一点:基本类型虽然也可以添加属性,也不会报错,经测试添加完之后却是无法访问的,实际上,javascript里的String是不可以改变的,javascript也没有提供任何一个改变字符串的方法和语法。

看下面的例子:

 var b = "myobject";
 b = b.substring(,);
 alert("b="+b); // b=bj

运行结果:

  

  这样做,没并有改变String字符串"myobject",只b引用了另一个字符串"bj","myobject"被回收了。

  所以可以这样讲,String实际上并不符合上面两种数据类型分类。它是具有两方面属性介于两都之间的一种特殊类型。

3.1.3、Null 类型

  Null类型只有一个专用值 null,值 undefined 实际上是从值 null 派生来的,因此 ECMAScript 把它们定义为相等的。

  <script type="text/javascript">
    alert("null == undefined的结果是:"+(null == undefined)); //输出 "true"
  </script>

运行结果:

  

  尽管这两个值相等,但它们的含义不同。undefined 是声明了变量但未对其初始化时赋予该变量的值,null 则用于表示尚未存在的对象(typeof 运算符对于 null 值会返回 "Object"。)。如果函数或方法要返回的是对象,那么找不到该对象时,返回的通常是 null。

3.1.4、Undefined 类型

  Undefined 类型只有一个值,即 undefined。当声明的变量未初始化时,该变量的默认值是 undefined。

var oTemp;

  前面一行代码声明变量 oTemp,没有初始值。该变量将被赋予值 undefined,即 undefined 类型的字面量。可以用下面的代码段测试该变量的值是否等于 undefined:

 <script type="text/javascript">
   var oTemp;
   alert("oTemp == undefined的结果是:"+(oTemp == undefined));//输出 "true"
  </script>

运行结果:

  

  运行结果显示 "true",说明这两个值确实相等。

  可以用 typeof 运算符显示该变量所代表的的数据类型是undefined类型

 <script type="text/javascript">
   var oTemp;
   alert("typeof oTemp的结果是:"+typeof oTemp); //输出 "undefined"
  </script>

  值 undefined 并不同于未定义的值。但是,typeof 运算符并不真正区分这两种值。考虑下面的代码:

 <script type="text/javascript">
   var oTemp;
   alert("oTemp变量有声明,typeof oTemp的结果是:"+typeof oTemp); //输出 "undefined"
   alert("oTemp变量没有声明,typeof oTemp的结果是:"+typeof oTemp); //输出 "undefined"
  </script>

运行结果:

  两个变量输出的都是 "undefined",即使只有变量 oTemp2 从未被声明过。如果对oTemp2 使用除 typeof 之外的其他运算符的话,会引起错误,因为其他运算符只能用于已声明的变量上。

  下面的代码将引发错误:

var oTemp;
 alert(oTemp == undefined);//'oTemp' 未定义

  当函数无明确返回值时,返回的也是值 "undefined",如下所示:

function testFunc() { 
   //这是一个空函数,没有返回值
 }
 alert("testFunc() == undefined的结果是:"+(testFunc() == undefined)); //输出 "true"

运行结果:

  

3.2、引用数据类型

  javascript引用数据类型是保存在堆内存中的对象,JavaScript不允许直接访问堆内存空间中的位置和操作堆内存空间,只能通过操作对象在栈内存中的引用地址。所以引用类型的数据,在栈内存中保存的实际上是对象在堆内存中的引用地址。通过这个引用地址可以快速查找到保存在堆内存中的对象。

看下面的例子:

 <script type="text/javascript">
   var obj = new Object();
   var obj = obj;
   obj.name = "孤傲苍狼";
   alert(obj.name); // 孤傲苍狼
 </script>

运行结果:

  

  由上面例子,我们声明了一个引用数据类型变量obj1,并把它赋值给了另外一个引用数据类型变量obj2。当我们obj2添加了一个name属性并赋值" 孤傲苍狼"。obj1同样拥有了和obj2一样的name属性。说明这两个引用数据类型变量指向同一个对象。obj1赋值给obj2,实际只是把这个对象在栈内存的引用地址复制了一份给了obj2,但它们本质上共同指向了堆内存中的同一个对象。

下面我们来演示这个引用数据类型赋值过程

    

自然,给obj2添加name属性,实际上是给堆内存中的对象添加了name属性,obj2和obj1在栈内存中保存的只是堆内存对象的引用地址,虽然也是拷贝了一份,但指向的对象却是同一个。故而改变obj2引起了obj1的改变。

  一般而言,基本数据类型是由固定数目的字节组成,这些字节可以在解析器的较底层进行操作,比如Number和 Boolean;而引用数据类型,可以包含任意数目的属性和元素,因此它们无法像基本数据类型那样很容易的操作。

由于,引用数据类型的值是会发生变化的, 所以通过跟基本数据类型一样的值传递方式,也就没什么意义了,因为会牵涉到大量的内存的复制和比较,效率太低。所以引用数据类型是通过引用传递方式,实际传递的只是对象的一个地址。

比如Array和Function,因为它们都是特殊的对象所以它们都是引用类型。另外,引用类型是可以添加属性,基本类型虽然也可以添加属性,也不会报错,经测试添加完之后却是无法访问的。

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
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.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

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

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.