查询
新增
let arr = ["马浩楠","程明辉"]
// 新增 :添加到数组的结尾 并返回长度
length = arr.push("李泽","吴赛")
document.write(length)
// 新增:添加到数组的开头 并返回长度
newLength = arr.unshift("楠","22")
document.write(arr)
document.write(newLength)
删除
let arr = ["马浩楠","程明辉","李泽","吴赛"]
// 1.pop删除最后一个元素,并返回删除的元素值
console.log(arr.pop()) // 吴赛
console.log(arr) // [ "马浩楠", "程明辉", "李泽" ]
// 2.shift删除第一个元素,并返回删除的元素值
console.log(arr.shift()) // 马浩楠
console.log(arr) // [ "程明辉", "李泽" ]
// 3.splice 删除指定元素 如果参数为一个,则表示该参数值位置后面全部删除
arr.splice(1,1)
console.log(arr) //["程明辉"]
let arr = [3,5,2,1,4]
console.log(arr.length)
for(let i = 0;i < arr.length;i++){
for(let j = i+1;j < arr.length;j++){
if(arr[i] < arr[j]){
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}
}
console.log(arr)
语法
function sayHi(){
document.write("hai~~")
}
函数传参
函数传参-默认值
function getSum(x = 0, y = 0){
document.write(x + y)
}
getSum() //结果是0,而不会是undefined + undefined = NaN
getSum(1, 2) // 结果是3
函数返回值
细节补充
作用域:与Java一致
匿名函数
let fn = function(x, y){
console.log(x + y)
}
fn(1, 2); // 3
// 2.匿名函数:第一种写法 【注意前后函数需要用;隔开】
(function () {
let num = 10
console.log(num) // 10
})();
(function(){
let num = 20
console.log(num) // 20
})();
// 3.匿名函数:第二种写法
(function(x, y){
console.log(x + y)
}(2, 1))
声明语法
let 对象名 = {}
let 对象名 = new Object()
对象属性 —— 查
let person = {
name: "马浩楠",
age: 22,
gender: "男"
}
console.log(person.name)
console.log(person["age"])
对象属性 —— 改/删
// 增
person.address = "河南省洛阳市"
// 改
person.name = "浩楠"
对象属性 —— 删
delete person.gender
对象方法
let person = {
name: "马浩楠",
sayHi: function(){
document.write("Hello")
}
}
遍历对象
let obj = {
uname: "马浩楠",
age: 22,
gender: "男"
}
// 遍历对象
for(let k in obj){
console.log(obj[k])
}
案例
<style>
table {
margin:0 auto;
width:400px;
text-align: center;
height:200px
}
h1 {
text-align: center;
}
</style>
<body>
<h1>学生列表</h1>
<table cellspacing="0" border="1px">
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>家乡</th>
</tr>
<script>
let students = [
{name:"小明",age:18,gender:"男",hometown:"河北省"},
{name:"小红",age:19,gender:"女",hometown:"河南省"},
{name:"小刚",age:17,gender:"男",hometown:"山西省"},
{name:"小丽",age:18,gender:"女",hometown:"山东省"}
]
for(let i = 0;i < students.length; i++){
document.write(`
<tr>
<td>${i+1}</td>
<td>${students[i].name}</td>
<td>${students[i].age}</td>
<td>${students[i].gender}</td>
<td>${students[i].hometown}</td>
</tr>
`)
}
document.write("</table>")
</script>
</body>
</html>
内置对象
JavaScript内部提供的对象,包含各种属性和方法给开发者调用
// Math属性
console.log(Math.PI)
// Math方法
// random 随机数 0-1之间
console.log(Math.random())
// 1.ceil()向上取整
console.log(Math.ceil(1,1))
// 2.floor()向下取整
console.log(Math.floor(1.1))
// 3.around()四舍五入
console.log(Math.round(1,4))
console.log(Math.round(1,5))
// 4.max()最大值
console.log(Math.max(1,2,3,4,5))
// 5.min()最小值
console.log(Math.min(1,2,3,5,6))
// 6.abs()取绝对值
console.log(Math.abs(-10))
内置对象,生成任意范围随机数
// 左闭右开[0,1) ——可以取到0但取不到1
console.log(Math.random())
// 0~10之间的整数
console.log(parseInt(Math.random()*11))
// 5~10之间的整数
console.log(parseInt(Math.random()*6)+5)
function getRandom(N, M){
return parseInt(Math.random()*(M - N +1))+N
}
console.log(getRandom(4,8))
简单类型又叫做基本数据类型或者值类型,复杂类型又叫做引用类型
值类型
引用类型
栈内存
简单数据类型存放在栈里面
堆内存
引用数据类型放在堆内存中