• 前端JavaScript入门到精通,javascript核心进阶ES6语法、API、js高级等基础知识和实战 —— JS基础(五)


    接受自己原本的样子,

    比努力扮演另一个轻松多了。

    思维导图

    对象

    什么是对象

    对象使用

    遍历对象

    索引号是字符串型,不推荐遍历数组。

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. <style>
    8. table {
    9. width: 600px;
    10. text-align: center;
    11. }
    12. table,
    13. th,
    14. td {
    15. border: 1px solid #ccc;
    16. border-collapse: collapse;
    17. }
    18. caption {
    19. font-size: 18px;
    20. margin-bottom: 10px;
    21. font-weight: 700;
    22. }
    23. tr {
    24. height: 40px;
    25. cursor: pointer;
    26. }
    27. table tr:nth-child(1) {
    28. background-color: #ddd;
    29. }
    30. table tr:not(:first-child):hover {
    31. background-color: #eee;
    32. }
    33. style>
    34. head>
    35. <body>
    36. <h2>学生信息h2>
    37. <p>将数据渲染到页面中...p>
    38. <table>
    39. <caption>学生列表caption>
    40. <tr>
    41. <th>序号th>
    42. <th>姓名th>
    43. <th>年龄th>
    44. <th>性别th>
    45. <th>家乡th>
    46. tr>
    47. <script>
    48. // 1. 数据准备
    49. let students = [
    50. { name: '小明', age: 18, gender: '男', hometown: '河北省' },
    51. { name: '小红', age: 19, gender: '女', hometown: '河南省' },
    52. { name: '小刚', age: 17, gender: '男', hometown: '山西省' },
    53. { name: '小丽', age: 18, gender: '女', hometown: '山东省' },
    54. { name: '晓强', age: 16, gender: '女', hometown: '蓝翔技校' }
    55. ]
    56. // 2. 渲染页面
    57. for (let i = 0; i < students.length; i++) {
    58. document.write(`
    59. ${i + 1}
    60. ${students[i].name}
    61. ${students[i].age}
    62. ${students[i].gender}
    63. ${students[i].hometown}
    64. `)
    65. }
    66. script>
    67. table>
    68. body>
    69. html>

    内置对象

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math

    关于round,如果是.5,往大取,-20.5,-20大取-20;4.5,往大取,5。

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. // 属性
    12. console.log(Math.PI)
    13. // 方法
    14. // ceil 天花板 向上取整
    15. console.log(Math.ceil(1.1)) // 2
    16. console.log(Math.ceil(1.5)) // 2
    17. console.log(Math.ceil(1.9)) // 2
    18. // floor 地板 向下取整
    19. console.log(Math.floor(1.1)) // 1
    20. console.log(Math.floor(1.5)) // 1
    21. console.log(Math.floor(1.9)) // 1
    22. console.log(Math.floor('12px')) // 1
    23. console.log('----------------')
    24. // 四舍五入 round
    25. console.log(Math.round(1.1)) // 1
    26. console.log(Math.round(1.49)) // 1
    27. console.log(Math.round(1.5)) // 2
    28. console.log(Math.round(1.9)) // 2
    29. console.log(Math.round(-1.1)) // -1
    30. console.log(Math.round(-1.5)) // -1
    31. console.log(Math.round(-1.51)) // -2
    32. // 取整函数 parseInt(1.2) // 1
    33. // 取整函数 parseInt('12px') // 12
    34. console.log(Math.max(1, 2, 3, 4, 5))
    35. console.log(Math.min(1, 2, 3, 4, 5))
    36. console.log(Math.abs(-1));
    37. // null 类似 let obj = {}
    38. let obj = null
    39. script>
    40. body>
    41. html>

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. // 左闭右开 能取到 0 但是取不到 1 中间的一个随机小数
    12. // console.log(Math.random())
    13. // 0~ 10 之间的整数
    14. // console.log(Math.floor(Math.random() * 11))
    15. // let arr = ['red', 'green', 'blue']
    16. // let random = Math.floor(Math.random() * arr.length)
    17. // // console.log(random)
    18. // console.log(arr[random])
    19. // 取到 N ~ M 的随机整数
    20. function getRandom(N, M) {
    21. return Math.floor(Math.random() * (M - N + 1)) + N
    22. }
    23. console.log(getRandom(4, 8))
    24. script>
    25. body>
    26. html>

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. let arr = ['赵云', '黄忠', '关羽', '张飞', '马超', '刘备', '曹操']
    12. // 1. 得到一个随机数, 作为数组的索引号, 这个随机数 0~6
    13. let random = Math.floor(Math.random() * arr.length)
    14. // 2. 页面输出数组里面的元素
    15. document.write(arr[random])
    16. // 3. splice(起始位置(下标), 删除几个元素)
    17. arr.splice(random, 1)
    18. console.log(arr)
    19. script>
    20. body>
    21. html>

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. // 1. 随机生成一个数字 1~10
    12. // 取到 N ~ M 的随机整数
    13. function getRandom(N, M) {
    14. return Math.floor(Math.random() * (M - N + 1)) + N
    15. }
    16. let random = getRandom(1, 10)
    17. console.log(random)
    18. // 需要不断的循环
    19. while (true) {
    20. // 2. 用户输入一个值
    21. let num = +prompt('请输入你猜的数字:')
    22. // 3. 判断输出
    23. if (num > random) {
    24. alert('您猜大了')
    25. } else if (num < random) {
    26. alert('您猜小了')
    27. } else {
    28. alert('猜对啦,真厉害')
    29. break // 退出循环
    30. }
    31. }
    32. script>
    33. body>
    34. html>

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. // 1. 随机生成一个数字 1~10
    12. // 取到 N ~ M 的随机整数
    13. function getRandom(N, M) {
    14. return Math.floor(Math.random() * (M - N + 1)) + N
    15. }
    16. let random = getRandom(1, 10)
    17. // 2. 设定三次 三次没猜对就直接退出
    18. let flag = true // 开关变量
    19. for (let i = 1; i <= 3; i++) {
    20. let num = +prompt('请输入1~10之间的一个数字:')
    21. if (num > random) {
    22. alert('您猜大了,继续')
    23. } else if (num < random) {
    24. alert('您猜小了,继续')
    25. } else {
    26. flag = false
    27. alert('猜对了,真厉害')
    28. break
    29. }
    30. }
    31. // 写到for的外面来
    32. if (flag) {
    33. alert('次数已经用完')
    34. }
    35. script>
    36. body>
    37. html>

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <style>
    9. div {
    10. width: 300px;
    11. height: 300px;
    12. }
    13. style>
    14. head>
    15. <body>
    16. <script>
    17. // 1. 自定义一个随机颜色函数
    18. function getRandomColor(flag = true) {
    19. if (flag) {
    20. // 3. 如果是true 则返回 #ffffff
    21. let str = '#'
    22. let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
    23. // 利用for循环随机抽6次 累加到 str里面
    24. for (let i = 1; i <= 6; i++) {
    25. // 每次要随机从数组里面抽取一个
    26. // random 是数组的索引号 是随机的
    27. let random = Math.floor(Math.random() * arr.length)
    28. // str = str + arr[random]
    29. str += arr[random]
    30. }
    31. return str
    32. } else {
    33. // 4. 否则是 false 则返回 rgb(255,255,255)
    34. let r = Math.floor(Math.random() * 256) // 55
    35. let g = Math.floor(Math.random() * 256) // 89
    36. let b = Math.floor(Math.random() * 256) // 255
    37. return `rgb(${r},${g},${b})`
    38. }
    39. }
    40. // 2. 调用函数 getRandomColor(布尔值)
    41. console.log(getRandomColor(false))
    42. console.log(getRandomColor(true))
    43. console.log(getRandomColor())
    44. // let str = '#'
    45. // str = str + 'f'
    46. script>
    47. body>
    48. html>

    综合案例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. head>
    9. <body>
    10. <script>
    11. let num1 = 10
    12. let num2 = num1
    13. num2 = 20
    14. console.log(num1) // 结果是?
    15. let obj1 = {
    16. age: 18
    17. }
    18. let obj2 = obj1
    19. // 修改属性
    20. obj2.age = 20
    21. console.log(obj1.age)
    22. script>
    23. body>
    24. html>

  • 相关阅读:
    中国大模型语料数据联盟迎来9家新成员,开源第二批语料数据
    行列视(RCV)数据安全如何保障?
    JavaScript: 异步代码同步执行
    15-k8s-高级存储之pv与pvc
    jquery 分页兼容i7,i8浏览器
    立晶半导体Cubic Lattice Inc 专攻音频ADC,音频DAC,音频CODEC,音频CLASS D等CL7016
    fanuc机器人IO分配报警信号分配无效
    dalle:zero-shot text-to-image generation
    java版工程管理系统Spring Cloud+Spring Boot+Mybatis实现工程管理系统源码
    Java中的代码优雅重构实战
  • 原文地址:https://blog.csdn.net/upgrade_bro/article/details/133231726