网站跨平台,wordpress添加赏,网站悬浮图标怎么做,移动网站开发面试# 本文详细结束了JavaScript中函数、对象、常用类Array#xff0c;String#xff0c;Math和Date的用法。 一、函数
1、概述 将程序中多次要用到的代码块封装起来#xff0c;就是函数。函数使代码块的重复使用更方便#xff0c;且功能独立#xff0c;便于维护。 2、函数的… # 本文详细结束了JavaScript中函数、对象、常用类ArrayStringMath和Date的用法。 一、函数
1、概述 将程序中多次要用到的代码块封装起来就是函数。函数使代码块的重复使用更方便且功能独立便于维护。 2、函数的定义与使用
①. 简介 函数可以使用参数来传递数据也可以不使用参数。函数在完成功能后可以有返回值也可以没有返回值。 function 函数名(参数1,参数2…)
{函数体return 返回值;
}function 函数名(参数1,参数2…)
{语句......return 表达式; //return语句指明被返回的值
} ②. 函数的使用
!DOCTYPE html
html langzh
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleJS的函数/title
/head
bodyscriptfunction calculator(a, b){var c ab;return c; }var result calculator(1,1);console.log(result result);/script
/body
/html
代码运行后如下 ③.在JS程序中被调用 包括有返回值和无返回值的调用
!DOCTYPE html
html langzh
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleJS的函数/title
/head
bodyscriptfunction calculator(a, b){c ab;alert(ab c); return c; }var result calculator(1,1);console.log(result result);// 1. 在JS程序中被调用 包括有返回值和无返回值的调用console.log(ab c)/script/body
/html
代码运行后如下 3、监听点击事件
①.在按钮被点击时调用
!DOCTYPE html
html langzh
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleJS的函数/title
/head
bodyscriptfunction calculator(a, b){c ab;alert(ab c); return c; }/script!-- 2. 在按钮或超链接被点击时调用监听点击事件 --!-- 2.1 监听按钮点击 --input typesubmit value计算ab的和 onclickcalculator(1,2)//body
/html
代码运行后如下 ②.在超链接被点击时调用
!DOCTYPE html
html langzh
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleJS的函数/title
/head
bodyscriptfunction calculator(a, b){c ab;alert(ab c); return c; }/script!-- 2.2 监听超链接点击 --a href# onclickcalculator(3,4)百度一下你就知道/a
/body
/html
代码运行后如下 4、变量 变量的作用域变量分为全局变量和局部变量。全局变量定义在所有函数之外作用范围是所有函数局部变量定义在函数之内只对该函数可见对其它函数不可见。 全局变量和局部变量的运用
!DOCTYPE html
html langzh
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleJS的函数/title
/head
bodyscriptvar c1; //全局变量既可以作用于所有函数内又可以作用于函数外function calculator1(a,b){ var d2; //局部变量只作用与函数内return (abc)*d; //返回6}rlt1 calculator1(1,1)console.log(计算结果为: rlt1)rlt2 (11c)*d //函数外无法调用局部变量d控制台报错 console.log(计算结果为: rlt2)/script/body
/html
代码运行后如下 二、对象
1、概述 对象object是 JavaScript 中最重要的数据类型是一组“键值对”的集合体。类似Python中的字典。其中键可以为变量名此时称为对象的属性和函数名此时称为对象的方法 2、对象的定义和使用 定义一个对象类似于python里面的类其结构是键值对使用一个对象的属性和方法 !DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlejs的对象/title
/head
bodyscript// 1.定义一个对象类似于python里面的类其结构是键值对var person{age:18,sex:female_,calculator:function(a,b){return ab;}}// 2.使用一个对象的属性和方法console.log(person.age) //使用对象的属性console.log(person.calculator(1,1))//使用对象的方法/script
/body
/html
代码运行后如下 3、js的常用对象
①.Array对象
1.根据Array对象得到一个对应的数组实例和往数组里面添加一个新的元素入栈
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlejs的常用对象/title
/head
bodyscript//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 langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlejs的常用对象/title
/head
bodyscript//1.根据Array对象得到一个对应的数组实例var arr new Array(11,22,33);length arr.push(zhangsan)//4.颠倒数组元素console.log(arr.reverse());/body
/html 代码运行后如下 3.获取数组中某个元素的索引
!DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlejs的常用对象/title
/head
bodyscript//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 aaavar b bbbvar c ccc
4.concat 用于顺序连接多个字符串返回一个新字符串 var d abcvar 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 langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlejs的内置对象Math/title
/head
bodyscript//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();
//获取年份四位数的年份比如2024const year now.getFullYear();
文章转载自: http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.rmyqj.cn.gov.cn.rmyqj.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.gqtw.cn.gov.cn.gqtw.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.fxzgw.com.gov.cn.fxzgw.com http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.hptbp.cn.gov.cn.hptbp.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.jlthz.cn.gov.cn.jlthz.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.lrwsk.cn.gov.cn.lrwsk.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.bylzr.cn.gov.cn.bylzr.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.kcrw.cn.gov.cn.kcrw.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.whclz.cn.gov.cn.whclz.cn http://www.morning.tftw.cn.gov.cn.tftw.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.xmttd.cn.gov.cn.xmttd.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.bttph.cn.gov.cn.bttph.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.pwppk.cn.gov.cn.pwppk.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.lmmkf.cn.gov.cn.lmmkf.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn