(一)引用数据类型

  1. object
  2. function
  3. array

object

JavaScript对象用花括号来书写
对象属性是name:value由逗号分隔

var x={firstname:"bill",lastname:"ddd"};

function

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
function add(a,b){
return a+b;
}
var x=add("ss","dd");
var s=add(1,2);
console.log(x);
console.log(typeof x);
console.log(s);
console.log(typeof s);
</script>
</body>
</html>

array

var arr={"aaa","ss","dd"};

特征

  1. 值可变
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var x={age:20};
x.age=21;
console.log(x.age);
console.log(typeof x.age);
</script>
</body>
</html>
  • 存放在栈和堆中

(二)数据类型检验方法

1.typeof

返回一个表示数据类型的字符串

  • number
  • boolean
  • string
  • symbol
  • object
  • undefined
  • function

不能判断array和null,以及date,regexp等

2.instanceof

用来判断A是否为B的实例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
console.log([]instanceof Array);
console.log({} instanceof Object);
console.log(new Date instanceof Date);
console.log(new RegExp instanceof RegExp);
</script>
</body>
</html>

无法判断字面量初始化的基本数据类型:

console.log(1 instanceof Number);//false
console.log(new Number(1) instanceof Number);//true

3.constructor

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
console.log((1).constructor===Number);
var a=[1,2];
console.log(a.constructor===Array);
console.log(a.constructor===Object);//false
var date=new Date()
console.log(date.constructor==Date);
</script>
</body>
</html>

constructor检测Object与instanceof不一样,只认当前数据类型,不认父类

  var a=[1,2];
console.log(a.constructor===Array);//true
console.log(a.constructor===Object);//false
console.log([]instanceof Array);//true
console.log([] instanceof Object);//true

4.总结

判断方法 缺点
typeof 不能判断array和null,以及date,regexp等
instanceof 无法判断字面量初始化的基本数据类型;null和undefined也无法被识别
constructor null和undefined也无法被识别

(三)运算符

  1. 检验密码长度(点击检验)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
密码:<input id="password" type="password" name="password" placeholder="密码长度在6-18之间">
<p><button type="button" onclick="checkpassword()">确定</button></p>
<script type="text/javascript">
function checkpassword(){
//获取密码
var password=document.getElementById("password").value;
//判断密码长度,不合格时弹框提示
if(password.length<6||password.length>18){
alert("密码长度在6-18之间,请重新输入");
}
}
</script>
</body>
</html>
  1. 检验用户名(时时检验)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
<style type="text/css">
span{
font-size: 10px;
color: red;
}
</style>
</head>
<body>
用户名:<input id="username" type="text" name="username" placeholder="请输入用户名" onkeyup="CheckUserName(this)">
<span id="msg"></span><br>
密码:<input id="password" type="password" name="password" placeholder="密码长度在6-18之间">
<p><button type="button" onclick="checkpassword()">确定</button></p>
<script type="text/javascript">
function checkpassword(){
//获取密码
var password=document.getElementById("password").value;
//判断密码长度,不合格时弹框提示
if(password.length<6||password.length>18){
alert("密码长度在6-18之间,请重新输入");
}
}
function CheckUserName(username){
var value=username.value;
/*使用弹窗*/
// if(value=="admin"){
// alert("不允许使用admin")
// }
/*使用插入*/
if(value=="admin"){
document.getElementById("msg").innerHTML="不允许使用admin";
}
else{
document.getElementById("msg").innerHTML="";
}
//简化
//document.getElementById("msg").innerHTML=(username.value=="admin"?"不允许使用admin":"");
}
</script>
</body>
</html>

1.算术运算符

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var n1=100+10+"100";
var n2="100"+10+100;
console.log(n1);
console.log(n2);
console.log(typeof n1);
console.log(typeof n2);
</script>
</body>
</html>

2.loop循环

  1. for…in…可以写下标
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var n1=["a","b","c","d","e"];
for(let x in n1){
console.log("第",x,"个:",n1[x]);
}
</script>
</body>
</html>
  1. for…of…(元素)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var n1=["a","b","c","d","e"];
for(let x of n1){
console.log(x);
}
</script>
</body>
</html>

(四)字符串

字符串

length

length 属性返回字符串的长度:

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
查找字符串中的字符串
  1. indexOf() 方法返回字符串中指定文本首次出现的索引(位置):
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");//17
  1. lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");//51
  1. 如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。
  2. 两种方法都接受作为检索起始位置的第二个参数。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>dem</title>
</head>
<body>
<script type="text/javascript">
var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China",18);
var po = str.indexOf("China", 3);//17
console.log(pos);
console.log(po);
</script>
</body>
</html>
  1. lastIndexOf() 方法向后进行检索(从尾到头),这意味着:假如第二个参数是 50,则从位置 50 开始检索,直到字符串的起点。
var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China", 50);
  1. search() 方法搜索特定值的字符串,并返回匹配的位置:
var str = "The full name of China is the People's Republic of China.";
var pos = str.search("China");
  1. 两种方法,indexOf() 与 search(),是相等的。
    这两种方法是不相等的。区别在于:
    search() 方法无法设置第二个开始位置参数。
    indexOf() 方法无法设置更强大的搜索值(正则表达式)。

提取部分字符串

有三种提取部分字符串的方法:

  • slice(start, end)
  • substring(start, end)
  • substr(start, length)
  1. slice() 方法
    slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。
    该方法设置两个参数:起始索引(开始位置),终止索引(结束位置
    个例子裁剪字符串中位置 7 到位置 13 的片段:
var str = "Apple, Banana, Mango";
var res = str.slice(7,13);

如果某个参数为负,则从字符串的结尾开始计数。
这个例子裁剪字符串中位置 -12 到位置 -6 的片段:

var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7);

如果省略第二个参数,则该方法将裁剪字符串的剩余部分:

var res = str.slice(7);
substring() 方法

substring() 类似于 slice()。
不同之处在于 substring() 无法接受负的索引。

var str = "Apple, Banana, Mango";
var res = str.substring(7,13);

如果省略第二个参数,则该 substring() 将裁剪字符串的剩余部分。

substr() 方法

substr() 类似于 slice()。
不同之处在于第二个参数规定被提取部分的长度。

var str = "Apple, Banana, Mango";
var res = str.substr(7,6);
var str = "Apple, Banana, Mango";
var res = str.substr(7);

替换字符串内容

replace() 方法用另一个值替换在字符串中指定的值:
实例

str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3School")

replace() 方法不会改变调用它的字符串。它返回的是新字符串。

默认地,replace() 只替换首个匹配:

转换为大写和小写

  1. 通过 toUpperCase() 把字符串转换为大写:
    实例
var text1 = "Hello World!";       // 字符串
var text2 = text1.toUpperCase(); // text2 是被转换为大写的 text1
  1. 通过 toLowerCase() 把字符串转换为小写:
    实例
var text1 = "Hello World!";       // 字符串
var text2 = text1.toLowerCase(); // text2 是被转换为小写的 text1

concat() 方法

concat() 连接两个或多个字符串:
实例

var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ",text2);

随机推荐

  1. Linux 命令:grub2-mkconfig

    检索这个命令的,肯定都知道 grub 是 bootloader 程序,用于引导系统启动.配置文件是 grub.conf,现在一般的 grub 版本是grub2. 当机器上安装有多个内核.或者多个操作系 ...

  2. VSCode中的快捷键

    VS Code中的快捷键 文件和编辑操作 Ctrl+S:保存文件 Ctrl+K S:保存全部文件 Ctrl+C:复制 Ctrl+V:粘贴 Ctrl+X:剪切 Ctrl+F:查找 Ctrl+H:替换 C ...

  3. Singleton Pattern 单例模式简介与 C# 示例【创建型】【设计模式来了】

    〇.简介 1.什么是单例模式? 一句话解释:   单一的类,只能自己来创建唯一的一个对象. 单例模式(Singleton Pattern)是日常开发中最简单的设计模式之一.这种类型的设计模式属于创建型 ...

  4. koa搭建nodejs项目并注册接口

    使用nodejs注册接口逻辑处理会比较复杂,直接通过express或者koa能够简化开发流程,这里记录用koa来搭建nodejs项目并注册接口,对koa不太熟悉的话可以参考这一篇.让nodejs开启服 ...

  5. 记一次MySql灾难性事件

    2023年8月8日,本来系一个风和日丽的夏天中的平凡一天,但这种平凡,注定住佢一定唔平凡,唉...现在回忆起都阵阵咁痛!!! 重要嘅事情讲三次,唔好手贱,唔好手贱,唔好手贱 事日,如常上班,本人系一名 ...

  6. 文心一言 VS 讯飞星火 VS chatgpt (81)-- 算法导论7.4 6题

    六.如果用go语言,考虑对 PARTITION 过程做这样的修改:从数组 A 中随机选出三个元素,并用这三个元素的中位数(即这三个元素按大小排在中间的值)对数组进行划分.求以a 的函数形式表示的.最坏 ...

  7. 《Web安全基础》03. SQL 注入

    @ 目录 1:简要 SQL 注入 2:MySQL 注入 2.1:信息获取 2.2:跨库攻击 2.3:文件读写 2.4:常见防护 3:注入方法 3.1:类型方法明确 3.2:盲注 3.3:编码 3.4: ...

  8. 使用 SQL 的方式查询消息队列数据以及踩坑指南

    背景 为了让业务团队可以更好的跟踪自己消息的生产和消费状态,需要一个类似于表格视图的消息列表,用户可以直观的看到发送的消息:同时点击详情后也能查到消息的整个轨迹. 消息列表 点击详情后查看轨迹 原理介 ...

  9. TIKZ全局样式设置

    tikz绘图引擎 TIKZ绘图引擎是基于tex实现,代码极其复杂,每次编写都要单独设置样式,甚是繁琐,如何能够全局设置呢? \begin{tikzpicture}[ auto, % 决策结点 deci ...

  10. K8s 多集群实践思考和探索

    作者:vivo 互联网容器团队 - Zhang Rong 本文主要讲述了一些对于K8s多集群管理的思考,包括为什么需要多集群.多集群的优势以及现有的一些基于Kubernetes衍生出的多集群管理架构实 ...