首页 > 其他分享 >JS04-对象

JS04-对象

时间:2024-03-15 21:30:29浏览次数:25  
标签:console log min 对象 age JS04 obj Math

对象

使用对象

  • 声明人对象

  • 属性和方法都要写在对象里面

let person = {  
	  uname:'甘雨',

      age:2000,

      sex:'女',

      sayHi: function(){

        console.log('Hi~~~~~~~~~')

      }

    }
  • 1.访问属性 对象.属性名

console.log(person.uname)

console.log(person.age)

  • 2.访问属性 对象.[‘属性名’]

console.log(person['sex'])

  • 调用方法 对象.方法名()

person.sayHi()

对象操作

        let obj = {
            uname:'小明',
            age:18
        }
        console.log(obj.age)
        // 修改 对象.属性 = 新值
        obj.age = 81
        console.log(obj.age)
        // 新增一个属性
        obj.sex = '男'
        obj.simg = function() {
            document.write('hi')
        }
        console.dir(obj)
        obj.simg()
        // 删除
        (function () {
            delete obj.age
            console.dir(obj)
        }())

遍历对象

    <script>
        let obj = {
            uname:'小明',
            age:18
        }
        // for (let k in 对象) {}
        // k变量 k 或者 key value
        for (let k in obj) {
            console.log(k) //属性名
            console.log(obj[k]) //属性值
        }

数学Math对象

        console.log(Math.PI)//圆周率
        console.log(Math.random())//随机数
        console.log(Math.ceil(1.2))//向上去整
        console.log(Math.floor(1.2))//向下去整
        console.log(Math.round(1.2))//就近去整(负数往大取-1.5)
        console.log(Math.max(1,2,654,54,4))

案例

学生信息表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table{
            width: 600px;
            text-align: center;
        }
        table,th,td{
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
        caption{
            font-size: 8px;
            margin-bottom: 10px;
            font-weight: 700;
        }
        tr{
            height: 40px;
            cursor: pointer;
        }
        table tr:nth-child(1){
            background-color: #ddd;
        }
        table tr:not(:first-child):hover{
            background: #ccc;
        }
    </style>
</head>
<body>
   
        
    
    <script>
        document.write(`
            <h2>学生信息</h2>
            <p>将数据渲染到页面中</p>
            <table>
                <caption>学生列表</caption>
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>家乡</th>
                </tr>
            `)
        let student = [
            { name : '刻晴', age: '18',gender: '女', hometown:'璃月' },
            { name : '香菱', age: '18',gender: '女', hometown:'璃月' },
            { name : '凝光', age: '22',gender: '女', hometown:'璃月' },
            { name : '甘雨', age: '2000',gender: '女', hometown:'璃月' },
            { name : '钟离', age: '5000',gender: '男', hometown:'璃月' },
        ]
        // 遍历数组
        for (let i = 0; i < student.length; i++){
            document.write(`
            <tr>
                <th>${i + 1}</th>
                <th>${student[i].name}</th>
                <th>${student[i].age}</th>
                <th>${student[i].gender}</th>
                <th>${student[i].hometown}</th>
            </tr>
            `)
        }
        document.write(`
        </table>
        `)
    </script>
</body>
</html>

随机数

function getRandomIntInclusive(min, max) {
   min = Math.ceil(min);
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min + 1)) + min; 
    //含最大值,含最小值
}
let str = ['刻晴','香菱', '凝光','甘雨','钟离']
console.log(str[getRandomIntInclusive(0,str.length - 1)])

随机点名

function getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min; 
    //含最大值,含最小值
}
let arr = ['刻晴','香菱', '凝光','甘雨','钟离']
let random = getRandomIntInclusive(0,arr.length - 1)
console.log(arr[random]);
arr.splice(random,1)//arr.splice(初始位置,删几个)

猜数字

        function getRandomIntInclusive(min, max) {
            min = Math.ceil(min);
            max = Math.floor(max);
            return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
        }
        let random = getRandomIntInclusive(1,10)
        while (true){
            let num = +prompt('输入一个数')
            if(random < num){
                alert('太大了')
            }
            if(random > num){
                alert('小了')
            }
            else{
                alert('正确')
                break
            }
        }

标签:console,log,min,对象,age,JS04,obj,Math
From: https://blog.csdn.net/colorsZeroz/article/details/136745482

相关文章

  • 对象池与享元模式
    在我人生的第一次面试中,面试官问我:可以举几个例子吗?关于Unity中可能用到的设计模式。我当时对设计模式的学习并没有很深入,因此回答了:对象池应该是享元模式。但很快遭到了他的否决,我再次提议,应该是享元,但他还是摇了摇头。毕竟是公司主程,我只能低下头表示,自己会回去看看。在9个......
  • 面向过程与面向对象你弄清楚了吗?
    1.面向过程与面向对象的区别面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描述某个事务在整个解决问题的步骤中的行为......
  • 1分钟带你学会Python面向对象基础语法
    1.类和对象python中的面向对象主要学习类和对象类:是多个具有特殊功能的个体的集合,例如:人类/猫类/犬类对象:在一个类中,一个具有特殊功能的个体,能够帮忙解决某件特定的事情,也被称为实例两者之间的关系:类是用于描述某一类对象的共同特征,而对象是类的具体的存在在程序中......
  • Java面向对象的一些学习笔记
    1.Private关键字:(1)private关键字是一个权限修饰符(2)可以修饰成员变量和成员方法(3)被private修饰的成员只能在本类中才能访问(4)针对private修饰的成员变量,如果需要被其他类使用,提供相应的操作(5)提供"setXxx(参数)"方法,用于给成员变量赋值,方法用public修饰(6)提供"getXxx(参数)......
  • 【PG】Ora2pg 数据库对象迁移顺序
    在将数据库对象从Oracle迁移到PostgreSQL时,以下是一个常见的迁移顺序建议:表:首先迁移表的结构和数据,因为其他对象(如索引、触发器和函数)可能依赖于表的存在。索引:迁移表之后,迁移索引。在PostgreSQL中创建与Oracle索引相对应的索引。触发器:迁移触发器。在PostgreSQL中创建与O......
  • 07对象的创建
    1<!DOCTYPEhtml>2<htmllang="en">3<head>4<metacharset="UTF-8">5<metaname="viewport"content="width=device-width,initial-scale=1.0">6<title>Document......
  • 6. 面向对象(重点)
    1面向对象1.1了解对象学习面向对象编程都先我们需要先思考三个问题:1.1.1面向对象的好处?Java作者詹姆斯.高斯林说过**万物皆对象**汽车的数据可以找汽车对象处理手机数据可以找手机对象处理学生的数据可以找学生对象处理使用面向对象编程符合人类思维习惯,就好比谁......
  • 【PG】对比两个数据中对象
    #!/bin/bash####################################################################################################################################Name:Compare2PostgresDBs##Description:Compare2PostgresDBsTables,Indexes,andFunctions.Incas......
  • 【PG】查看PG对象大小增长情况
    #!/bin/bash####################################################check_postgresql_db_table_rowsnum_and_sizing.sh##Thisscriptsiteratesartidatabasesand#populatessometargetdbtablewith#infoabouttablesrownumandsizing##Date:10-Aug-......
  • Go语言中的面向对象编程(OOP)
    在Go语言中,虽然没有像面向对象语言那样的类,但通过结构体类型和方法,仍然支持部分面向对象编程(OOP)的概念。封装(Encapsulation)封装是一种将一个对象的实现细节隐藏起来,使其对其他对象不可见的做法,这样可以实现解耦。例如,考虑以下结构体:typeStudentstruct{namestring......