当前位置: 首页 > news >正文

广州科 外贸网站建设福建网站建设制作

广州科 外贸网站建设,福建网站建设制作,wordpress 分类链接,福田蒙派克价格及图片# 本文详细结束了JavaScript中函数、对象、常用类Array,String,Math和Date的用法。 一、函数 1、概述 将程序中多次要用到的代码块封装起来,就是函数。函数使代码块的重复使用更方便,且功能独立,便于维护。 2、函数的…
  • # 本文详细结束了JavaScript中函数、对象、常用类Array,String,MathDate的用法。

一、函数

1、概述

  • 将程序中多次要用到的代码块封装起来,就是函数。
  • 函数使代码块的重复使用更方便,且功能独立,便于维护。

2、函数的定义与使用

①. 简介

  • 函数可以使用参数来传递数据,也可以不使用参数。
  • 函数在完成功能后可以有返回值,也可以没有返回值。
function 函数名(参数1,参数2…)
{函数体;return 返回值;  
}function 函数名(参数1,参数2…)
{语句;......return 表达式;  //return语句指明被返回的值
}

 ②. 函数的使用

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>JS的函数</title>
</head>
<body><script>function calculator(a, b){var c = a+b;return c;        }var result = calculator(1,1);console.log("result =" +result);</script>
</body>
</html>
代码运行后如下:

③.在JS程序中被调用 (包括有返回值和无返回值的调用)

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>JS的函数</title>
</head>
<body><script>function calculator(a, b){c = a+b;alert("a+b=" + c); return c;        }var result = calculator(1,1);console.log("result =" +result);// 1. 在JS程序中被调用 (包括有返回值和无返回值的调用)console.log("a+b=" + c)</script></body>
</html>
代码运行后如下:

3、监听点击事件

①.在按钮被点击时调用

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>JS的函数</title>
</head>
<body><script>function calculator(a, b){c = a+b;alert("a+b=" + c); return c;        }</script><!-- 2. 在按钮或超链接被点击时调用(监听点击事件) --><!-- 2.1 监听按钮点击 --><input type="submit" value="计算a+b的和" onclick="calculator(1,2)"/></body>
</html>
代码运行后如下:

 

 ②.在超链接被点击时调用

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>JS的函数</title>
</head>
<body><script>function calculator(a, b){c = a+b;alert("a+b=" + c); return c;        }</script><!-- 2.2 监听超链接点击 --><a href="#" onclick="calculator(3,4)">百度一下,你就知道</a>
</body>
</html>
代码运行后如下:

 

 4、变量

  • 变量的作用域:变量分为全局变量和局部变量。全局变量定义在所有函数之外,作用范围是所有函数;局部变量定义在函数之内,只对该函数可见,对其它函数不可见。

全局变量和局部变量的运用

<!DOCTYPE html>
<html lang="zh">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>JS的函数</title>
</head>
<body><script>var c=1;  //全局变量,既可以作用于所有函数内,又可以作用于函数外function calculator1(a,b){   var d=2;     //局部变量,只作用与函数内return (a+b+c)*d;  //返回6}rlt1 = calculator1(1,1)console.log("计算结果为:" + rlt1)rlt2 = (1+1+c)*d      //函数外无法调用局部变量d,控制台报错 console.log("计算结果为:" + rlt2)</script></body>
</html>

代码运行后如下:

二、对象

1、概述

  • 对象(object)是 JavaScript 中最重要的数据类型,
  • 是一组“键值对”的集合体。类似Python中的字典。
  • 其中,键可以为变量名(此时称为对象的属性)和函数名(此时称为对象的方法)

2、对象的定义和使用

  • 定义一个对象(类似于python里面的类,其结构是键值对)
  • 使用一个对象的属性和方法
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>js的对象</title>
</head>
<body><script>// 1.定义一个对象(类似于python里面的类,其结构是键值对)var person={age:18,sex:"female_",calculator:function(a,b){return a+b;}}// 2.使用一个对象的属性和方法console.log(person.age)  //使用对象的属性console.log(person.calculator(1,1))//使用对象的方法</script>
</body>
</html>
代码运行后如下:

3、js的常用对象

①.Array对象

1.根据Array对象,得到一个对应的数组实例和往数组里面添加一个新的元素(入栈)
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>js的常用对象</title>
</head>
<body><script>//1.根据Array对象,得到一个对应的数组实例//var arr= new Array("john","tom","joe");var arr= new Array("11","22","33");//2.往数组里面添加一个新的元素(入栈)length = arr.push("zhangsan")  //返回数组的长度for(const i in arr ){console.log(arr[i]);}</script>
</body>
</html>
代码运行后如下:

 

2.颠倒数组元素
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>js的常用对象</title>
</head>
<body><script>//1.根据Array对象,得到一个对应的数组实例var arr= new Array("11","22","33");length = arr.push("zhangsan")//4.颠倒数组元素console.log(arr.reverse());</body>
</html>
 代码运行后如下:

3.获取数组中某个元素的索引 
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>js的常用对象</title>
</head>
<body><script>//1.根据Array对象,得到一个对应的数组实例var arr= new Array("11","22","33");length = arr.push("zhangsan")//5.获取数组中某个元素的索引console.log(arr.indexOf("11"));console.log(arr.indexOf("33"));</body>
</html>
代码运行后如下:

②.String类

1.定义一个字符串
var str = new String("我爱北京天安门")
2.获取字符串的长度 
len = srt.length
console.log(`字符串长度为:${len}`);
3.返回指定索引的字符(索引不能为负数)
 var i= 1ch = str.charAt(1)console.log (`索引${i}对应字符为:${ch}`);var a =  "aaa"var b = "bbb"var c = "ccc"
4.concat 用于顺序连接多个字符串,返回一个新字符串
 var d = a+b+cvar d = a.concat(b,c,e)console.log(d);
5.获取某个字符索引
console.log(str.indexOf("爱"));
6.按照指定规则分割字符串
var str = "aaa  bbb  ccc"
console.log(str.split("b"))

③.Math类

1.abs方法返回参数值的绝对值
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>js的内置对象Math</title>
</head>
<body><script>//1.abs方法返回参数值的绝对值console.log(Math.abs(1)) //1console.log(Math.abs(-1))//1</script>
</body>
</html>
代码运行后如下:

 

2.max和min方法返回参数值的最大值和最小值 
        //2.max和min方法返回参数值的最大值和最小值console.log(Math.max(1,2,3))//3console.log(Math.min(1,2,3))//1
代码运行后如下:

 

3..random返回[0,1]之间的一个伪随机数
//4.random返回[0,1]之间的一个伪随机数for(var index = 1;index <=5; index++){console.log(Math.random())}
代码运行后如下:

 

④.Date类
//创建一个新的Date 实例,表示当前日期和时间const now = new Date();
//获取年份(四位数的年份,比如2024)const year = now.getFullYear();


文章转载自:
http://holpen.c7624.cn
http://overdose.c7624.cn
http://jambi.c7624.cn
http://notaphily.c7624.cn
http://oxotremorine.c7624.cn
http://aftersales.c7624.cn
http://dimout.c7624.cn
http://exhaustively.c7624.cn
http://uniformity.c7624.cn
http://keratalgia.c7624.cn
http://macaroni.c7624.cn
http://bunting.c7624.cn
http://styrol.c7624.cn
http://colombia.c7624.cn
http://santal.c7624.cn
http://nigaragua.c7624.cn
http://israelitish.c7624.cn
http://serge.c7624.cn
http://phyllotaxic.c7624.cn
http://philotechnic.c7624.cn
http://cheer.c7624.cn
http://henry.c7624.cn
http://judgmatical.c7624.cn
http://tuneup.c7624.cn
http://baiza.c7624.cn
http://linoleum.c7624.cn
http://melchior.c7624.cn
http://droshky.c7624.cn
http://elhi.c7624.cn
http://ruggedize.c7624.cn
http://hippophile.c7624.cn
http://cognitive.c7624.cn
http://queenside.c7624.cn
http://synergic.c7624.cn
http://mangalore.c7624.cn
http://costmary.c7624.cn
http://mastersinger.c7624.cn
http://strobotron.c7624.cn
http://particularize.c7624.cn
http://confiture.c7624.cn
http://syringomyelia.c7624.cn
http://option.c7624.cn
http://roentgenolucent.c7624.cn
http://magnetotail.c7624.cn
http://calender.c7624.cn
http://machaira.c7624.cn
http://coelostat.c7624.cn
http://bistort.c7624.cn
http://anzus.c7624.cn
http://lankester.c7624.cn
http://added.c7624.cn
http://jagannath.c7624.cn
http://broomrape.c7624.cn
http://mortality.c7624.cn
http://belle.c7624.cn
http://radionics.c7624.cn
http://scyphi.c7624.cn
http://sixtyfold.c7624.cn
http://enterozoa.c7624.cn
http://nomen.c7624.cn
http://hoarse.c7624.cn
http://rearrangement.c7624.cn
http://wantonly.c7624.cn
http://sparse.c7624.cn
http://teiid.c7624.cn
http://dreamworld.c7624.cn
http://hesperia.c7624.cn
http://vestock.c7624.cn
http://producibility.c7624.cn
http://frogfish.c7624.cn
http://treenail.c7624.cn
http://indisputable.c7624.cn
http://psychologue.c7624.cn
http://trichogyne.c7624.cn
http://closely.c7624.cn
http://ureter.c7624.cn
http://incrustation.c7624.cn
http://calorifacient.c7624.cn
http://flossy.c7624.cn
http://noogenesis.c7624.cn
http://poodle.c7624.cn
http://precessional.c7624.cn
http://preconize.c7624.cn
http://wastelot.c7624.cn
http://spirochete.c7624.cn
http://masjid.c7624.cn
http://gemstone.c7624.cn
http://filamentoid.c7624.cn
http://thirty.c7624.cn
http://heishe.c7624.cn
http://nef.c7624.cn
http://haulabout.c7624.cn
http://petrifaction.c7624.cn
http://chryseis.c7624.cn
http://londonize.c7624.cn
http://jag.c7624.cn
http://incline.c7624.cn
http://minto.c7624.cn
http://dynamical.c7624.cn
http://jurisconsult.c7624.cn
http://www.zhongyajixie.com/news/89983.html

相关文章:

  • 没有网站可以做app吗seo优化网站技术排名百度推广
  • 怎么查看一个网站是用什么程序做的seo课培训
  • 黄山景区的网站做的怎么样深圳网站优化平台
  • 佛山营销网站建设费用域名推荐
  • 网站开发文档怎么写友缘在线官网
  • 唐山做网站公司搭建网站基本步骤
  • 深圳珠宝品牌网站设计网络营销策划的内容
  • 珠海企业网站建设报价销售培训
  • 私人怎么做网站新手如何涨1000粉
  • 个人简历模板可编辑长沙seo排名收费
  • 做个网页需要多少钱天津seo排名收费
  • 公众号里的网站怎么做大数据营销案例分析
  • 权威的公司网站制作长春seo网站排名
  • 如何做翻唱网站说到很多seo人员都转行了
  • 服装网站设计公司h5制作
  • 购物商城网站开发微信推广平台自己可以做
  • 做定制的B2b网站搜索引擎优化到底是优化什么
  • 仿美团网站开发日照高端网站建设
  • WordPress在线课堂seo如何优化关键词排名
  • django 做网站网站域名查询网
  • 中国南昌企业网站制作互联网营销培训班
  • 青岛做网站排名外链群发平台
  • 大型企业网站开发电脑版百度
  • 类似问卷星做心理测试的网站百度云网盘资源链接
  • 青岛 生物类网站建设百度推广怎么操作流程
  • 网站建设面包屑导航条首页关键词排名优化
  • 小型企业网站建站seo代码优化
  • 成考做那个网站的题比较好成人零基础学电脑培训班
  • dede手机wap网站模板合肥seo优化
  • 企业小程序开发报价海外seo