JavaScript内置对象
<p><span style="font-size: 15px">#1.内置对象</span></p><p><span style="font-size: 15px">1.JavaScript 中的对象分为3中:自定义对象、内置对象、浏览器对象</span></p>
<p><span style="font-size: 15px">2.前面两种对象是js 基础内容,属于 ECMAScript;第三个浏览器对象属于我们的js独有的,我们js API讲解</span></p>
<p><span style="font-size: 15px">3.内置对象就是指js语言自带的一些对象,这些对象供开发者使用,并提供了一些常用的或是最基本而必要的功能(属性和方法)</span></p>
<p><span style="font-size: 15px">4.内置对象最大的优点就是帮助我们快速开发</span></p>
<p><span style="font-size: 15px">5.JavaScript提供了多个内置对象:Math、Date、Array、Strting等</span></p>
<p><span style="font-size: 15px">#2.Math对象</span></p>
<p><span style="font-size: 15px">1.Math是一个内置对象,它具有数学常数和函数的属性和方法。不是一个函数对象<br></span></p>
<p><span style="font-size: 15px">2.与其他全局对象不同的是,Math不是一个构造函数,Math的所有属性和方法都是静态的,不需要是用 new 。</span></p>
<p><span style="font-size: 15px">案例:封装自己的数学对象</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript"><span style="color: rgba(0, 0, 0, 1)">
let myMath </span>=<span style="color: rgba(0, 0, 0, 1)"> {
PI: </span>3.1415926535<span style="color: rgba(0, 0, 0, 1)">,
max: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
let max </span>= arguments;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 1; i < arguments.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arguments ><span style="color: rgba(0, 0, 0, 1)"> max) {
max </span>=<span style="color: rgba(0, 0, 0, 1)"> arguments
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> max;
},
min: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
let min </span>= arguments;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 1; i < arguments.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arguments <<span style="color: rgba(0, 0, 0, 1)"> min) {
min </span>=<span style="color: rgba(0, 0, 0, 1)"> arguments
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> min;
}
}
console.log(myMath.PI);
console.log(myMath.max(</span>1, 5, 9<span style="color: rgba(0, 0, 0, 1)">));
console.log(myMath.min(</span>1, 5, 9<span style="color: rgba(0, 0, 0, 1)">));
</span></script></pre>
</div>
<p><span style="font-size: 15px">2.1Math 概述</span></p>
<p><span style="font-size: 15px">Math 对象不是构造函数,它具有数学常数和函数的属性和方法。</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript">
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.绝对值方法</span>
console.log(Math.ads(1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
console.log(Math.ads(-1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
console.log(Math.ads('-1')); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 隐式转换 会把字符串型 -1 转换为数字型</span>
console.log(Math.ads('pink')); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> NaN</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.三个取整方法</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">(1) Math.floor() 地板 向下取整 往最小了取值</span>
console.log(Math.floor(1.1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
console.log(Math.floor(1.9)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">(2) Math.ceil() ceil 向上取整 往最大了取值</span>
console.log(Math.ceil(1.1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2</span>
console.log(Math.ceil(1.9)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">(3) Math.round() 四舍五入</span>
console.log(Math.round(1.1)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1</span>
console.log(Math.round(1.9)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2</span>
<span style="font-size: 15px"> </script></span></pre>
</div>
<p><span style="font-size: 15px">2.2随机数方法 random()</span></p>
<p><span style="font-size: 15px">1.Math.random()函数返回一个随机的小数</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript"><span style="color: rgba(0, 0, 0, 1)">
console.log(Math.random());
</span></script></pre>
</div>
<p><span style="font-size: 15px">2.我们想要得到两个数之间的随机整数 并且包含这两个整数</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript">
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getRandom(max, min) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> Math.floor(Math.random() * (max - min + 1)) +<span style="color: rgba(0, 0, 0, 1)"> min;
}
console.log(getRandom(</span>1, 10<span style="color: rgba(0, 0, 0, 1)">));
</span></script></pre>
</div>
<p><span style="font-size: 15px">案例:猜数字游戏</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript">
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getRandom(max, min) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> Math.floor(Math.random() * (max - min + 1)) +<span style="color: rgba(0, 0, 0, 1)"> min;
}
let random </span>= getRandom(1, 10<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">while</span>(<span style="color: rgba(0, 0, 255, 1)">true</span>) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这是一个死循环</span>
let num = prompt('请输入1~10之间的一个数'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (num ><span style="color: rgba(0, 0, 0, 1)"> random) {
alert(</span>'猜大了'<span style="color: rgba(0, 0, 0, 1)">)
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(num <<span style="color: rgba(0, 0, 0, 1)"> random) {
alert(</span>'猜小了'<span style="color: rgba(0, 0, 0, 1)">)
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
alert(</span>'对了'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
</span></script></pre>
</div>
<p><span style="font-size: 15px">#3.日期对象</span></p>
<p><span style="font-size: 15px">1.Date对象</span></p>
<p><span style="font-size: 15px">创建Date实例用来处理日期和时间,Date 对象基于1970年1月1日起的毫秒数</span></p>
<p><span style="font-size: 15px">Date 是一个构造函数,必须使用new 来调用创建我们的日期对象</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript">
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.使用date 如果没有参数 返回当前系统的当前时间</span>
let date = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
console.log(date);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.参数常用的写法 数字型 2022, 10 , 01 或者是 字符串型 '2022-10-1 8:8:8'</span>
let date1 = <span style="color: rgba(0, 0, 255, 1)">new</span> Date(2022, 10, 1<span style="color: rgba(0, 0, 0, 1)">);
console.log(date1);
let date2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Date('2022-10-1 8:8:8'<span style="color: rgba(0, 0, 0, 1)">)
console.log(date2);
</span></script></pre>
</div>
<p><span style="font-size: 15px">3.1Date 概述</span></p>
<p>1.Date 对象和 Math 对象不一样,他是一个构造函数,所以我们需要实例化后在能用</p>
<p>2.Data 实例主要用来处理日期和时间的</p>
<p><span style="font-size: 15px">3.2Date()方法的使用</span></p>
<p>1.获取当前时间必须实例化</p>
<div class="cnblogs_code">
<pre>let date = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
console.log(date);</span></pre>
</div>
<p>2.<span style="font-size: 15px">Date()构造函数的参数</span></p>
<p><span style="font-size: 15px">如果括号里有时间,就返回参数里面的时间,参数有两种写法,一种数字型一种字符串型</span></p>
<p><span style="font-size: 15px">3.3 日期格式化</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220409165127390-710704305.png" alt="" width="718" height="301" loading="lazy"></span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript">
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 格式化日期 年月日</span>
let date = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
console.log(date.getFullYear()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回当前日期的年份</span>
console.log(date.getMonth() + 1); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回当前日期的月份 返回的月份要小一个月 记着月份加1</span>
console.log(date.getDate()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回的是 几号</span>
console.log(date.getDay()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回今天是周几 注意 周日返回的是0</span>
</script></pre>
</div>
<p><span style="font-size: 15px">1.时分秒</span></p>
<p> 获取时分秒<span style="font-size: 15px"><br></span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript"><span style="color: rgba(0, 0, 0, 1)">
let date </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
console.log(date.getHours()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取当前小时</span>
console.log(date.getMinutes()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取当前时间的分钟</span>
console.log(date.getSeconds()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取当前时间的秒数</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 要求封装一个函数返回当前的时分秒 格式:08:08:08</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getTime() {
let time </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
let h </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getHours();
h </span>= h < 10 ? '0' +<span style="color: rgba(0, 0, 0, 1)"> h : h
let m </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getMinutes();
m </span>= m < 10 ? '0' +<span style="color: rgba(0, 0, 0, 1)"> m : m
let s </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getSeconds();
s </span>= s < 10 ? '0' +<span style="color: rgba(0, 0, 0, 1)"> s : s
</span><span style="color: rgba(0, 0, 255, 1)">return</span> h + ':' + m + ':' +<span style="color: rgba(0, 0, 0, 1)"> s;
}
console.log(getTime());
</span></script></pre>
</div>
<p><span style="font-size: 15px">3.4.获取日期的总的毫秒形式</span></p>
<p><span style="font-size: 15px">所以我们可以通过我们的Date对象来获取总的毫秒数</span></p>
<p><span style="font-size: 15px">三种方法 1. valueof()2.getTime()3.直接创建变量 new Date()</span></p>
<div class="cnblogs_code">
<pre><script type="text/javascript">
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取date总毫秒数 不是当前毫秒数 而是距离1970年1月1号过了多少毫秒</span>
let date = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();
console.log(date.valueOf()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 方法一 就是我们现在的时间距离1970.1.1的总毫秒数</span>
console.log(date.getTime()); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 方法二</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2. 简单的写法 (是我们最常用的写法)</span>
let date1 = +<span style="color: rgba(0, 0, 255, 1)">new</span> Date(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> +new Date() 返回的就是总的毫秒数</span>
<span style="color: rgba(0, 0, 0, 1)"> console.log(date1);
</span></script></pre>
</div>
<p><span style="font-size: 15px">#4.数组对象</span></p>
<p><span style="font-size: 15px">4.1数组对象的创建</span></p>
<p><span style="font-size: 15px">创建数组对象的两种方式</span></p>
<p><span style="font-size: 15px">1.字面量创建</span></p>
<div class="cnblogs_code">
<div>
<div><script></div>
<div> let arr = ;</div>
<div> console.log(arr);</div>
<div> </script></div>
</div>
</div>
<p><span style="font-size: 15px">2.new Array()</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.利用new Array()</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> let arr1 = new Array()// 创建了一个空数组</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> let arr1 = new Array(2) // 这个2表示数组的长度为2里面有2个空的数组元素</span>
let arr1 = <span style="color: rgba(0, 0, 255, 1)">new</span> Array(2, 3); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 等价于 这样写表示 里面有2个数组元素 是 2和3</span>
<span style="color: rgba(0, 0, 0, 1)"> console.log(arr1);
</span></script></pre>
</div>
<p><span style="font-size: 15px">4.2检测是否为数组</span></p>
<p><span style="font-size: 15px">1.instanceof 运算符 它可以用来检测是否为数组</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> reverse(arr) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Array) {
let newArr </span>=<span style="color: rgba(0, 0, 0, 1)"> [];
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = arr.length - 1; i >= 0; i--<span style="color: rgba(0, 0, 0, 1)">) {
newArr </span>=<span style="color: rgba(0, 0, 0, 1)"> arr
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> newArr;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> '这个参数必须是数组格式'<span style="color: rgba(0, 0, 0, 1)">
}
}
console.log(reverse([</span>1, 2, 3<span style="color: rgba(0, 0, 0, 1)">]));
</span><span style="color: rgba(0, 0, 255, 1)">var</span> arr =<span style="color: rgba(0, 0, 0, 1)"> [];
let obj </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
console.log(arr </span><span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Array);
console.log(obj </span><span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> Array);
</span></script></pre>
</div>
<p><span style="font-size: 15px">2. Array.isArray(参数)</span></p>
<div class="cnblogs_code">
<pre><script><span style="color: rgba(0, 0, 0, 1)">
let arr </span>=<span style="color: rgba(0, 0, 0, 1)"> [];
let obj </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
console.log(Array.isArray(arr));
console.log(Array.isArray(obj));
</span></script></pre>
</div>
<p><span style="font-size: 15px">4.3.添加删除数组元素的方法</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220411095909702-230364929.png" alt="" width="832" height="226" loading="lazy"></span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 添加或删除数组的方法</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1.push() 在我们数组末尾 添加一个或多个数组元素 push 推</span>
let arr = ;
arr.push(</span>4, 'pink'<span style="color: rgba(0, 0, 0, 1)">);
console.log(arr);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (1) push() 是可以给数组追加新的元素</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (2) push() 参数直接写数组元素就可以了</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (3) push() 完毕之后,返回的结果是 新数组的长度</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (4) 原数组也会发生变化</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2.unshift 在我们数组开头 添加一个或多个数组元素</span>
arr.unshift('red', 'purple'<span style="color: rgba(0, 0, 0, 1)">);
console.log(arr);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (1) unshift() 是可以给数组前面追加新的元素</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (2) unshift() 参数直接写数组元素就可以了</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (3) unshift() 完毕之后,返回的结果是 新数组的长度</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (4) 原数组也会发生变化</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 删除数组元素</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3.pop() 它可以删除数组最后的一个元素</span>
<span style="color: rgba(0, 0, 0, 1)"> arr.pop();
console.log(arr);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (1) pop() 是可以删除数组最后的一个元素 记住一次只能删除一个元素</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (2) pop() 没有参数</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (3) pop() 完毕之后,返回的结果是 删除的那个元素,删除谁就返回谁</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (4) 原数组也会发生变化</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 4.shift() 它可以删除数组的第一个元素</span>
<span style="color: rgba(0, 0, 0, 1)"> arr.shift();
console.log(arr);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (1) shift() 是可以删除数组的第一个元素 记住一次只能删除一个元素</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (2) shift() 没有参数</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (3) shift() 完毕之后,返回的结果是 删除的那个元素,删除谁就返回谁</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (4) 原数组也会发生变化</span>
</script></pre>
</div>
<p><span style="font-size: 15px">案例:筛选数组</span></p>
<div class="cnblogs_code">
<pre><script><span style="color: rgba(0, 0, 0, 1)">
let arr </span>= ;
let arr1 </span>=<span style="color: rgba(0, 0, 0, 1)"> []
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i < arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr < 2000<span style="color: rgba(0, 0, 0, 1)">) {
arr1.push(arr)
}
}
console.log(arr1);
</span></script></pre>
</div>
<p><span style="font-size: 15px">4.4.数组排序</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220411102503448-2004174854.png" alt="" width="868" height="124" loading="lazy"></span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">数组排序</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1. 翻转数组</span>
let arr = ['pink', 'red', 'bule'<span style="color: rgba(0, 0, 0, 1)">];
arr.reverse();
console.log(arr);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2.数组排序 (冒泡排序)</span>
let arr1 = ;
arr1.sort((a, b) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> return a - b; // 升序的顺序排列</span>
<span style="color: rgba(0, 0, 255, 1)">return</span> b - a; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 降序的顺序排列</span>
<span style="color: rgba(0, 0, 0, 1)"> });
console.log(arr1);
</span></script></pre>
</div>
<p><span style="font-size: 15px">4.5.数组索引方法</span></p>
<p><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220411145125611-1243775206.png" alt="" width="841" height="121" loading="lazy"></p>
<p> </p>
<p> </p>
<p> <span style="font-size: 15px">indexOf() 方法</span></p>
<p><span style="font-size: 15px">从前向后查找</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回数组元素的索引方法</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> indexOf(数组元素) 作用就是返回该数组元素的索引号</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> let arr = ['red', 'green', 'blue', 'pink'];</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 他只返回第一个满足条件的索引号 </span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> let arr = ['red', 'green', 'blue', 'pink', 'blue'];</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 他如果在该数组当中找不到元素,则返回 -1</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> let arr = ['red', 'green', 'pink'];</span>
console.log(arr.indexOf('blue'<span style="color: rgba(0, 0, 0, 1)">))
</span></script></pre>
</div>
<p><span style="font-size: 15px">lastIndexOf() 方法</span></p>
<p><span style="font-size: 15px">从后向前查找</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> lastIndexOf(数组元素) 作用就是返回该数组元素的索引号</span>
let arr = ['red', 'green', 'blue', 'pink'<span style="color: rgba(0, 0, 0, 1)">];
console.log(arr.lastIndexOf(</span>'blue'<span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 他如果在该数组当中找不到元素,则返回 -1</span>
let arr = ['red', 'green', 'pink'<span style="color: rgba(0, 0, 0, 1)">];
console.log(arr.lastIndexOf(</span>'blue'<span style="color: rgba(0, 0, 0, 1)">))
</span></script></pre>
</div>
<p><span style="font-size: 15px">案例:数组去重</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> unique(arr) {
let arr1 </span>=<span style="color: rgba(0, 0, 0, 1)"> [];
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i < arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr1.indexOf(arr) === -1<span style="color: rgba(0, 0, 0, 1)">) {
arr1.push(arr)
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arr1;
}
let demo </span>= unique(['c', 'a', 'z', 'a', 'x', 'a', 'x', 'c', 'b'<span style="color: rgba(0, 0, 0, 1)">])
console.log(demo)
</span></script></pre>
</div>
<p><span style="font-size: 15px">4.6 数组转换为字符串</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220411152623926-1636490415.png" alt="" width="877" height="126" loading="lazy"></span></p>
<div class="cnblogs_code">
<pre> <script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> split() </span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 把一个字符串分割成字符串数组</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> a = "How are you doing today?"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> b = a.split(" "<span style="color: rgba(0, 0, 0, 1)">);
console.log(b);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> join()</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 把数组中的所有元素转换为一个字符串:</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> c = ["Banana", "Orange", "Apple", "Mango"<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d =<span style="color: rgba(0, 0, 0, 1)"> c.join();
console.log(d);
</span></script></pre>
</div>
<p><span style="font-size: 15px">#5.字符串对象</span></p>
<p><span style="font-size: 15px">5.1基本包装类型</span></p>
<p><span style="font-size: 15px">为了方便操作基本数据类型,JavaScript还提供了三个特殊的引用类型:String、Number和Boolean。</span></p>
<p><span style="font-size: 15px">基本包装类型就是把简单数据类型包装成为复杂数据类型,这样基本数据类型就有了属性和方法。</span></p>
<p><span style="font-size: 15px">5.2 字符串的不可变</span></p>
<p><span style="font-size: 15px">因为我们字符串的不可变所以不要大量的拼接字符串</span></p>
<p><span style="font-size: 15px">5.3 根据字符返回位置</span></p>
<p><span style="font-size: 15px">字符串所有的方法都不会修改字符串本身</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220411154728059-236782666.png" alt="" width="827" height="145" loading="lazy"></span></p>
<p> </p>
<p> </p>
<p> 两个方法用法一样,只不过一个是从前向后查找,一个是从后向前查找</p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 字符串对象,根据 字符串返回位置 str.indexOf('要查找的字符,[起始位置]')</span>
let str = '改革春风吹满地,春风来了'<span style="color: rgba(0, 0, 0, 1)">;
console.log(str.indexOf(</span>'春'<span style="color: rgba(0, 0, 0, 1)">))
console.log(str.indexOf(</span>'春', 3)) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 从索引3的位置开始往后查找</span>
</script></pre>
</div>
<p><span style="font-size: 15px">案例:返回字符串位置</span></p>
<div class="cnblogs_code">
<div>
<div><script></div>
<div> let str = "abcoefoxyozzopp";</div>
<div> let index = str.indexOf('o');</div>
<div> let num = 0;</div>
<div> console.log(index);</div>
<div> while (index != -1) {</div>
<div> console.log(index);</div>
<div> num++;</div>
<div> index = str.indexOf('o', index + 1)</div>
<div> }</div>
<div> console.log("o出现的次数是:" + num);</div>
<div> </script></div>
</div>
</div>
<p><span style="font-size: 15px">5.4 根据位置返回字符</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220411160952907-101700368.png" alt="" width="903" height="170" loading="lazy"></span></p>
<p> </p>
<p> </p>
<p> 方法一 charAt(index) 根据位置返回字符串<span style="font-size: 15px"><br></span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1. charAt(index) 根据位置返回字符串</span>
let str = 'andy'<span style="color: rgba(0, 0, 0, 1)">
console.log(str.charAt(</span>3<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历所有的字符</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i < str.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
console.log(str.charAt(i));
}
</span></script></pre>
</div>
<p><span style="font-size: 15px">方法二 charCodeAt(index) 返回相应索引号的字符ASCII值 目的:判断用户按下了那个键</span></p>
<div class="cnblogs_code">
<pre><script><span style="color: rgba(0, 0, 0, 1)">
let str </span>= 'andy'<span style="color: rgba(0, 0, 0, 1)">
console.log(str.charCodeAt(</span>0<span style="color: rgba(0, 0, 0, 1)">));
</span></script></pre>
</div>
<p>方法三 str H5新增的</p>
<div class="cnblogs_code">
<pre><script><span style="color: rgba(0, 0, 0, 1)">
let str </span>= 'andy'<span style="color: rgba(0, 0, 0, 1)">
console.log(str[</span>0<span style="color: rgba(0, 0, 0, 1)">]);
</span></script></pre>
</div>
<p>案例:返回字符串位置</p>
<div class="cnblogs_code">
<pre><script><span style="color: rgba(0, 0, 0, 1)">
let str </span>= "abcoefoxyozzopp"<span style="color: rgba(0, 0, 0, 1)">;
let a </span>=<span style="color: rgba(0, 0, 0, 1)"> {};
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i < str.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
let chars </span>=<span style="color: rgba(0, 0, 0, 1)"> str.charAt(i)
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (a) {
a</span>++<span style="color: rgba(0, 0, 0, 1)">;
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
a </span>= 1<span style="color: rgba(0, 0, 0, 1)">;
}
}
console.log(a);
let max </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
let ch </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let k <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> a) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (a ><span style="color: rgba(0, 0, 0, 1)"> max) {
max </span>=<span style="color: rgba(0, 0, 0, 1)"> a
ch </span>=<span style="color: rgba(0, 0, 0, 1)"> k
}
}
console.log(max);
console.log(</span>'最多字符的是' +<span style="color: rgba(0, 0, 0, 1)"> ch);
</span></script></pre>
</div>
<p><span style="font-size: 15px"> 5.5字符串操作方法</span></p>
<p><span style="font-size: 15px"><img src="https://img2022.cnblogs.com/blog/2811223/202204/2811223-20220412085019459-186459549.png" alt="" width="875" height="236" loading="lazy"></span></p>
<p> </p>
<p><span style="font-size: 15px"></span></p>
<p> <span style="font-size: 15px">方法1:concat</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1.concat('字符串1','字符串2'······)</span>
let str = 'andy'<span style="color: rgba(0, 0, 0, 1)">
console.log(str.concat(</span>'red'<span style="color: rgba(0, 0, 0, 1)">));
</span></script></pre>
</div>
<p><span style="font-size: 15px">方法2:substr</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2. substr('截取的起始位置','截取几个字符')</span>
let str1 = '改革春风吹满地'<span style="color: rgba(0, 0, 0, 1)">
console.log(str1.substr(</span>2, 2)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一个2是索引号2,第二个2是截取长度的2</span>
</script></pre>
</div>
<p><span style="font-size: 15px">方法3 replace</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <script>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 3.替换字符 replace('被替换的字符','替换为的字符') 他只会替换第一个字符</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> let str = 'andy'
<span style="color: rgba(0, 128, 128, 1)">4</span> console.log(str.replace('a', 'b'<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">5</span> </script></pre>
</div>
<p><span style="font-size: 15px">方法4 split</span></p>
<div class="cnblogs_code">
<pre><script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 2. 字符串转换为数组 split('分割字符')</span>
let str = 'red, pink, blue'<span style="color: rgba(0, 0, 0, 1)">;
console.log(str.split(</span>','<span style="color: rgba(0, 0, 0, 1)">));
</span></script></pre>
</div>
<p>扩展方法</p>
<p> toUpperCase() 转换大写</p>
<p>toLowerCase() 转换小写</p><br><br>
来源:https://www.cnblogs.com/zzm666/p/16123150.html
頁:
[1]