当前位置: 首页 > 编程日记 > 正文

转:45 Useful JavaScript Tips, Tricks and Best Practices

原文来自于:http://flippinawesome.org/2013/12/23/45-useful-javascript-tips-tricks-and-best-practices/

1 – Don’t forget var keyword when assigning a variable’s value for the first time.

Assignment to an undeclared variable automatically results in a global variable being created. Avoid global variables.

2 – use === instead of ==

The == (or !=) operator performs an automatic type conversion if needed. The === (or!==) operator will not perform any conversion. It compares the value and the type, which could be considered faster than ==.

[10] === 10    // is false
[10]  == 10    // is true
'10' == 10     // is true
'10' === 10    // is false[]   == 0     // is true[] ===  0     // is false'' == false   // is true but true == "a" is false'' ===   false // is false 

3 – undefinednull, 0, falseNaN'' (empty string) are all falsy.
4 – Use Semicolons for line termination

The use of semi-colons for line termination is a good practice. You won’t be warned if you forget it, because in most cases it will be inserted by the JavaScript parser.

5 – Create an object constructor

function Person(firstName, lastName){this.firstName =  firstName;this.lastName = lastName;        
}  var Saad = new Person("Saad", "Mousliki");

6 – Be careful when using typeofinstanceof and constructor.

var arr = ["a", "b", "c"];
typeof arr;   // return "object" 
arr  instanceof Array // true
arr.constructor();  //[]

7 – Create a Self-calling Function

This is often called a Self-Invoked Anonymous Function or Immediately Invoked Function Expression (IIFE). It is a function that executes automatically when you create it, and has the following form:

(function(){// some private code that will be executed automatically
})();  
(function(a,b){var result = a+b;return result;
})(10,20)

8 – Get a random item from an array

var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];var  randomItem = items[Math.floor(Math.random() * items.length)];

9 – Get a random number in a specific range

This code snippet can be useful when trying to generate fake data for testing purposes, such as a salary between min and max.

var x = Math.floor(Math.random() * (max - min + 1)) + min;

10 – Generate an array of numbers with numbers from 0 to max

var numbersArray = [] , max = 100;for( var i=1; numbersArray.push(i++) < max;);  // numbers = [0,1,2,3 ... 100] 

11 – Generate a random set of alphanumeric characters

function generateRandomAlphaNum(len) {var rdmstring = "";for( ; rdmString.length < len; rdmString  += Math.random().toString(36).substr(2));return  rdmString.substr(0, len);}

12 – Shuffle an array of numbers

var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
numbers = numbers.sort(function(){ return Math.random() - 0.5});
/* the array numbers will be equal for example to [120, 5, 228, -215, 400, 458, -85411, 122205]  */

13 – A string trim function

The classic trim function of Java, C#, PHP and many other language that remove whitespace from a string doesn’t exist in JavaScript, so we could add it to the Stringobject.

String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "");};  

14 – Append an array to another array

var array1 = [12 , "foo" , {name "Joe"} , -2458];var array2 = ["Doe" , 555 , 100];
Array.prototype.push.apply(array1, array2);
/* array1 will be equal to  [12 , "foo" , {name "Joe"} , -2458 , "Doe" , 555 , 100] */

15 – Transform the arguments object into an array

var argArray = Array.prototype.slice.call(arguments);

16 – Verify that a given argument is a number

function isNumber(n){return !isNaN(parseFloat(n)) && isFinite(n);
}

17 – Verify that a given argument is an array

function isArray(obj){return Object.prototype.toString.call(obj) === '[object Array]' ;
}

Note that if the toString() method is overridden, you will not get the expected result using this trick.

Or use…

Array.isArray(obj); // its a new Array method

You could also use instanceof if you are not working with multiple frames. However, if you have many contexts, you will get a wrong result.

var myFrame = document.createElement('iframe');
document.body.appendChild(myFrame);var myArray = window.frames[window.frames.length-1].Array;
var arr = new myArray(a,b,10); // [a,b,10]  // instanceof will not work correctly, myArray loses his constructor 
// constructor is not shared between frames
arr instanceof Array; // false

18 – Get the max or the min in an array of numbers

var  numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411]; 
var maxInNumbers = Math.max.apply(Math, numbers); 
var minInNumbers = Math.min.apply(Math, numbers);

19 – Empty an array

var myArray = [12 , 222 , 1000 ];  
myArray.length = 0; // myArray will be equal to [].

20 – Don’t use delete to remove an item from array

Use split instead of using delete to delete an item from an array. Using deletereplaces the item with undefined instead of the removing it from the array.

Instead of… >

var items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
delete items[3]; // return true 
items.length; // return 11 
/* items will be equal to [12, 548, "a", undefined × 1, 5478, "foo", 8852, undefined × 1, "Doe", 2154,       119]   */

Use…

var items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
items.splice(3,1) ; 
items.length; // return 10 
/* items will be equal to [12, 548, "a", 5478, "foo", 8852, undefined × 1, "Doe", 2154,       119]   */

The delete method should be used to delete an object property.

21 – Truncate an array using length

Like the previous example of emptying an array, we truncate it using the length property.

var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];  
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].

As a bonus, if you set the array length to a higher value, the length will be changed and new items will be added with undefined as a value. The array length is not a read only property.

myArray.length = 10; // the new array length is 10 
myArray[myArray.length - 1] ; // undefined

22 – Use logical AND/ OR for conditions

var foo = 10;  
foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething(); 
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();

The logical AND could also be used to set a default value for function argument.

Function doSomething(arg1){ Arg1 = arg1 || 10; // arg1 will have 10 as a default value if it’s not already set
}

23 – Use the map() function method to loop through an array’s items

var squares = [1,2,3,4].map(function (val) {  return val * val;  
}); 
// squares will be equal to [1, 4, 9, 16] 

24 – Rounding number to N decimal place

var num =2.443242342;
num = num.toFixed(4);  // num will be equal to 2.4432

25 – Floating point problems

0.1 + 0.2 === 0.3 // is false 
9007199254740992 + 1 // is equal to 9007199254740992  
9007199254740992 + 2 // is equal to 9007199254740994

Why does this happen? 0.1 +0.2 is equal to 0.30000000000000004. What you need to know is that all JavaScript numbers are floating points represented internally in 64 bit binary according to the IEEE 754 standard. For more explanation, take a look to this blog post.

You can use toFixed() and toPrecision() to resolve this problem.

26 – Check the properties of an object when using a for-in loop

This code snippet could be useful in order to avoid iterating through the properties from the object’s prototype.

for (var name in object) {  if (object.hasOwnProperty(name)) { // do something with name                    }  
}

27 – Comma operator

var a = 0; 
var b = ( a++, 99 ); 
console.log(a);  // a will be equal to 1 
console.log(b);  // b is equal to 99

28 – Cache variables that need calculation or querying

In the case of a jQuery selector, we could cache the DOM element.

var navright = document.querySelector('#right'); 
var navleft = document.querySelector('#left'); 
var navup = document.querySelector('#up'); 
var navdown = document.querySelector('#down');

29 – Verify the argument before passing it to isFinite()

isFinite(0/0) ; // false 
isFinite("foo"); // false 
isFinite("10"); // true 
isFinite(10);   // true 
isFinite(undifined);  // false 
isFinite();   // false 
isFinite(null);  // true  !!! 

30 – Avoid negative indexes in arrays

var numbersArray = [1,2,3,4,5]; 
var from = numbersArray.indexOf("foo") ;  // from is equal to -1 
numbersArray.splice(from,2);    // will return [5]

Make sure that the arguments passed to indexOf are not negative.

31 – Serialization and deserialization (working with JSON)

var person = {name :'Saad', age : 26, department : {ID : 15, name : "R&D"} }; 
var stringFromPerson = JSON.stringify(person); 
/* stringFromPerson is equal to "{"name":"Saad","age":26,"department":{"ID":15,"name":"R&D"}}"   */ 
var personFromString = JSON.parse(stringFromPerson);  
/* personFromString is equal to person object  */

32 – Avoid the use of eval() or the Function constructor

Use of eval or the Function constructor are expensive operations as each time they are called script engine must convert source code to executable code.

var func1 = new Function(functionCode);
var func2 = eval(functionCode);

33 – Avoid using with() (The good part)

Using with() inserts a variable at the global scope. Thus, if another variable has the same name it could cause confusion and overwrite the value.

34 – Avoid using for-in loop for arrays

Instead of using…

var sum = 0;  
for (var i in arrayNumbers) {  sum += arrayNumbers[i];  
}

…it’s better to use…

var sum = 0;  
for (var i = 0, len = arrayNumbers.length; i < len; i++) {  sum += arrayNumbers[i];  
}

As a bonus, the instantiation of i and len is executed once because it’s in the first statement of the for loop. Thsi is faster than using…

for (var i = 0; i < arrayNumbers.length; i++)

Why? The length of the array arrayNumbers is recalculated every time the loop iterates.

35 – Pass functions, not strings, to setTimeout() and setInterval()

If you pass a string into setTimeout() or setInterval(), the string will be evaluated the same way as with eval, which is slow. Instead of using…

setInterval('doSomethingPeriodically()', 1000);  
setTimeOut('doSomethingAfterFiveSeconds()', 5000);

…use…

setInterval(doSomethingPeriodically, 1000);  
setTimeOut(doSomethingAfterFiveSeconds, 5000);

36 – Use a switch/case statement instead of a series of if/else

Using switch/case is faster when there are more than 2 cases, and it is more elegant (better organized code). Avoid using it when you have more than 10 cases.

37 – Use switch/case statement with numeric ranges

Using a switch/case statement with numeric ranges is possible with this trick.

function getCategory(age) {  var category = "";  switch (true) {  case isNaN(age):  category = "not an age";  break;  case (age >= 50):  category = "Old";  break;  case (age <= 20):  category = "Baby";  break;  default:  category = "Young";  break;  };  return category;  
}  
getCategory(5);  // will return "Baby"

38 – Create an object whose prototype is a given object

It’s possible to write a function that creates an object whose prototype is the given argument like this…

function clone(object) {  function OneShotConstructor(){}; OneShotConstructor.prototype= object;  return new OneShotConstructor(); 
} 
clone(Array).prototype ;  // []

39 – An HTML escaper function

function escapeHTML(text) {  var replacements= {"<": "&lt;", ">": "&gt;","&": "&amp;", "\"": "&quot;"};                      return text.replace(/[<>&"]/g, function(character) {  return replacements[character];  }); 
}

40 – Avoid using try-catch-finally inside a loop

The try-catch-finally construct creates a new variable in the current scope at runtime each time the catch clause is executed where the caught exception object is assigned to a variable.

Instead of using…

var object = ['foo', 'bar'], i;  
for (i = 0, len = object.length; i <len; i++) {  try {  // do something that throws an exception }  catch (e) {   // handle exception  } 
}

…use…

var object = ['foo', 'bar'], i;  
try { for (i = 0, len = object.length; i <len; i++) {  // do something that throws an exception } 
} 
catch (e) {   // handle exception  
} 

41 – Set timeouts to XMLHttpRequests

You could abort the connection if an XHR takes a long time (for example, due to a network issue), by using setTimeout() with the XHR call.

var xhr = new XMLHttpRequest (); 
xhr.onreadystatechange = function () {  if (this.readyState == 4) {  clearTimeout(timeout);  // do something with response data }  
}  
var timeout = setTimeout( function () {  xhr.abort(); // call error callback  
}, 60*1000 /* timeout after a minute */ ); 
xhr.open('GET', url, true);  xhr.send();

As a bonus, you should generally avoid synchronous Ajax calls completely.

42 – Deal with WebSocket timeout

Generally when a WebSocket connection is established, a server could time out your connection after 30 seconds of inactivity. The firewall could also time out the connection after a period of inactivity.

To deal with the timeout issue you could send an empty message to the server periodically. To do this, add these two functions to your code: one to keep alive the connection and the other one to cancel the keep alive. Using this trick, you’ll control the timeout.

Add a timerID

var timerID = 0; 
function keepAlive() { var timeout = 15000;  if (webSocket.readyState == webSocket.OPEN) {  webSocket.send('');  }  timerId = setTimeout(keepAlive, timeout);  
}  
function cancelKeepAlive() {  if (timerId) {  cancelTimeout(timerId);  }  
}

The keepAlive() function should be added at the end of the onOpen() method of the webSocket connection and the cancelKeepAlive() at the end of the onClose() method.

43 – Keep in mind that primitive operations can be faster than function calls. UseVanillaJS.

For example, instead of using…

var min = Math.min(a,b); 
A.push(v);

…use…

var min = a < b ? a b; 
A[A.length] = v;

44 – Don’t forget to use a code beautifier when coding. Use JSLint and minification (JSMin, for example) before going live.

45 – JavaScript is awesome: Best Resources To Learn JavaScript

Conclusion

I know that there are many other tips, tricks and best practices, so if you have any ones to add or if you have any feedback or corrections to the ones that I have shared, please adda comment.

References

In this article I have used my own code snippets. Some of the snippets are inspired from other articles and forums:

  • JavaScript Performance Best Practices (CC)
  • Google Code JavaScript tips
  • StackOverFlow tips and tricks
  • TimeOut for XHR

转载于:https://www.cnblogs.com/guoyongrong/p/3508212.html

相关文章:

聊聊spring cloud gateway的PreserveHostHeaderGatewayFilter

序 本文主要研究下spring cloud gateway的PreserveHostHeaderGatewayFilter GatewayAutoConfiguration spring-cloud-gateway-core-2.0.0.RC2-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java Configuration ConditionalOnProperty(name…

静态测试与测试计划

文章目录1 静态测试2 评审2.1 what2.2 why2.3 形式2.4 分类2.4.1 属于软件测试的部分2.4.2 属于软件质量保证的部分&#xff1a;3 需求测试3.1 why3.2 需求中可能存在的问题3.3 需求文档检查要点3.3.1 完整性3.3.2 正确性3.3.3 一致性3.3.4 可行性3.3.5 无二义型3.3.6 健壮性3.…

中国HBase技术社区第一届Meetup资料大合集

2018年6月6号&#xff0c;由中国HBase技术社区组织&#xff0c;阿里云主办的中国第一次HBase Meetup在北京望京阿里中心举行&#xff0c;来自阿里、小米、滴滴、360等公司的各位HBase的PMC、committer共聚一堂&#xff0c;共同探讨HBase2.0的技术革新以及HBase在国内各个大型企…

寻找历史!!!

“你一点都不了解中国”。在大约四年的时间里&#xff0c;我几乎每天都听到类似的批 评。每周一的中午&#xff0c;我坐着红色的出租车沿着三环路前往上班地点。尽管北京拥有 世界上最宽阔的道路&#xff0c;但在早晨与傍晚时&#xff0c;那些亨利福特&#xff34;型车的后代们…

wikioi 1083 Cantor表

找规律题 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的&#xff1a; 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … 3/1 3/2 3/3 … 4/1 4/2 … 5/1 … … 我们以Z字形给上表的每一项编号。第一项是1/1&#xff0c;然后是1/…

hung-yi lee_p3_线性回归

文章目录本节目的解决过程损失函数解损失函数&#xff08;by梯度下降&#xff09;改进模型矫枉过正解决方案本课结论本节目的 找到这样一个函数&#xff0c;输入宝可梦当前的CP(Combat Point)值&#xff0c;得到它进化后的CP值。 解决过程 损失函数 函数的函数&#xff1a;衡…

PHP简单封装MysqlHelper类

MysqlHelper.class.php 1: <?php 2: 3: /** 4: * Mysql数据帮助类 5: */ 6: class MysqlHelper 7: { 8: function __construct() 9: { 10: if(isset($conn)){return;} 11: //创建连接对象 12: $this->connmysql_connect($this->…

python之XML文件解析

python对XML的解析 常见的XML编程接口有DOM和SAX&#xff0c;这两种接口处理XML文件的方式不同&#xff0c;当然使用场合也不同。 python有三种方法解析XML&#xff0c;分别是SAX&#xff0c;DOM&#xff0c;以及ElementTree三种方法。 以下案例依次介绍三种方法&#xff1a; 先…

这句话真他妈经典

研究解决一个问题的时候&#xff0c;通常花百分之二十的时间和精力&#xff0c;就能抓住问题的百分之八十&#xff0c;而为了完善那余下的百分之二十&#xff0c;却往往要花百分之八十的时间和精力 这句话真他妈经典&#xff0c;呵呵 转载于:https://www.cnblogs.com/webcool…

hung-yi lee_p4_Bias And Variance

文章目录本节目的biasvariance结论&#xff08;鱼和熊掌不可得兼&#xff09;如何解决减小bias的方案减小variance的方案对训练集进行处理得到更好的模型本节目的 where does the error come from?&#xff08;为什么最复杂的模型反而Loss函数的值越大&#xff09; error有…

感觉 Data Access Application Block(DAAB) 里也有可能写得不太好的地方

昨天下载了博客园的代码&#xff0c;里面有一个Data\SqlServer.cs我不清楚是不是 MS DAAB 里的原样文件。不过前面有声明如下&#xff1a;////Microsoft Data Access Application Block for .NET 3.0////SqlServer.cs////This file contains the implementations of the AdoHel…

微软压力测试工具 web application stress

WEB服务器的压力测试工具~ 115808 2009年8月1日lbimba 铜牌会员 这里给广大的煤油推荐一个web网站压力测试工具。它可以用来模拟多个用户操作网站&#xff0c;在程序投入运行时&#xff0c;可以用它来进行程序的测试并得到Web站点的稳定 参数&#xff0c;甚至于可以对一台小型的…

Didn't find class net.oschina.app.AppContext on

原因 你引入的Lib 未打钩 然后在 菜单Project -> Properties -> Java Build Path -> Order & Export, 然后选中你未打钩的, 然后菜单 Project->Clean&#xff0c;然后运行程序即可。转载于:https://blog.51cto.com/12237592/2129523

hung-yi lee_p5-7_Gradient Descent(梯度下降)

原视频地址 https://www.bilibili.com/video/BV1JE411g7XF?p5 文章目录梯度下降是如何优化函数的tips1. 使用Adagrad2. Stochastic Gradient Descent3. Feature Scaling梯度下降理论基础梯度下降的局限性梯度下降是如何优化函数的 前情回顾&#xff1a;损失函数是用来衡量找到…

第九章 9.2 数组的方法(Array Methods)

注&#xff1a;这里只讲解一些 Array() 的最重要的方法。其他更多的参考手册。9.2.1 join() 将所有元素转换为字符串并默认用 "," 连接。可以指定一个附加的参数来自定义分隔符&#xff1a; vara [1, 2, 3];vars a.join(); //s "1,2,3"s a.join(", &…

HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap

http://www.importnew.com/8658.html转载于:https://www.cnblogs.com/passer1991/p/3520563.html

php7+的php-fpm参数配置,注意事项

安装php7的&#xff0c;如果php-fpm的这几个参数设置不当了&#xff0c;会导致php-fpm启动不了&#xff0c;nginx站点不能解析php文件&#xff0c;报404错误。 相关命令&#xff1a;centos7&#xff0c;启动php-fpm&#xff1a; systemctl start php-fpm查看php-fpm是否启动&am…

hung-yi lee_p10_分类/概率生成模型

文章目录研究背景本节目的本节要使用的例子研究过程把分类当成回归来算理想做法找到最佳函数的方法研究成果运用运用过程结果方法改进模型总结讨论为什么选择正态分布模型&#xff1f;关于后验概率的求法之化简与改进猜想一张图总结研究背景 本节目的 Classification:Probabi…

【跃迁之路】【495天】程序员高效学习方法论探索系列(实验阶段252-2018.06.15)...

(跃迁之路)专栏 实验说明 从2017.10.6起&#xff0c;开启这个系列&#xff0c;目标只有一个&#xff1a;探索新的学习方法&#xff0c;实现跃迁式成长实验期2年&#xff08;2017.10.06 - 2019.10.06&#xff09;我将以自己为实验对象。我将开源我的学习方法&#xff0c;方法不断…

wpf+xml实现的一个随机生成早晚餐的小demo

话说每到吃完的时间就发愁&#xff0c;真的不知道该吃什么&#xff0c;然后就想到做一个生成吃什么的小软件&#xff0c;既然这个软件如此的简单&#xff0c;就打算用wpf开发吧&#xff0c;也不用数据库了&#xff0c;直接保存在xml中就可以了 程序整体结构如下图 首先我写了一…

CentOS报错:TNS-12541: TNS:no listener TNS-12560: TNS:protocol adapter error TNS-00511: No listener

问题描述 原因 listener.ora中的ORACLE_HOME错了 解决 这个错误当时是和另一条指令lsnrctl start的错误一起报的&#xff0c;那个已解决&#xff0c;详细做法请各位移步我的另一篇博客 https://blog.csdn.net/weixin_44997802/article/details/109266708

c#数据结构———二叉查找树

using System;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />namespace BinaryTreeLibrary{///创建的是二叉查找树&#xff0c;没有重复的结点值///特点&#xff1a;左支树中任何值都小于父结点值&#xff0c;右结点任何值大于…

Spring事务管理只对出现运行期异常进行回滚

使用spring难免要用到spring的事务管理&#xff0c;要用事务管理又会很自然的选择声明式的事务管理&#xff0c;在spring的文档中说道&#xff0c;spring声明式事务管理默认对非检查型异常和运行时异常进行事务回滚&#xff0c;而对检查型异常则不进行回滚操作。那么什么是检查…

struts学习笔记三-国际化

在程序设计领域&#xff0c;人们把能够在无需改写有关代码的前提下&#xff0c;让开发出来的应用程序能够支持多种语言和数据格式的技术称为国际化技术。 国际化简称为 i18n&#xff0c;根据internationalization简化而来。 本地化简称为l10n&#xff0c;根据localization简化而…

TNS-01201: Listener cannot find executable /u01/oracle/bin/extproc for SID orcl Listener failed to

文章目录问题描述原因解决过程结果问题描述 原因 listener.ora文件中ORACLE_HOME的路径错了&#xff0c;导致按照这个路径找不到extproc 解决过程 首先去找ORACLE_HOME的路径 先切换为root用户&#xff08;这样查找时不会有文件夹进不去&#xff09; 输入指令 su root然后…

与技术无关的书单--你可以笑着说有些是“精神鸦片”

??? 转载于:https://www.cnblogs.com/crmhf/p/3823130.html

隐马尔科夫模型HMM(一)HMM模型

2019独角兽企业重金招聘Python工程师标准>>> 隐马尔科夫模型&#xff08;Hidden Markov Model&#xff0c;以下简称HMM&#xff09;是比较经典的机器学习模型了&#xff0c;它在语言识别&#xff0c;自然语言处理&#xff0c;模式识别等领域得到广泛的应用。当然&am…

stella forum v 2.0 的两款主题样式

stella forum v 2.0 的开发工作已经快结束啦&#xff0c;现在我正在加紧努力&#xff0c;想在本周内完成&#xff0c;因为下个星期我可能会不在学校。 下面公开一下我在做的两款主题&#xff0c;第一个是以前v1 版用的经典论坛的样式&#xff0c;而下面的第二款来自一个我很喜欢…

startup mount报错:invalid value given for the diagnostic_dest init.ora parameter

问题描述 解决思路 找到错误控制信息输出路径下的init文件 &#xff08;我的不知道为什么叫做initORCL.ora&#xff09; 将其中的ORACLE_BASE修改为正确路径 解决过程 输入指令 vi /db/app/oracle/product/11.2.0/dbs/initORCL.ora将其中三个涉及到ORACLE_BASE的地方该为正…

java的常用包

java.applet&#xff1a; 包含一些用于创建Java小应用程序的类。运行于html页面中。java.awt &#xff1a;包含一些用于编写与平台无关的图形界面&#xff08;GUI&#xff09;应用程序的类。java.io&#xff1a;包含一些用作输入输出&#xff08;I/O&#xff09;处理的类。java…