首页 > 技术文章 > JS对象练习

aimiao 2021-06-11 10:58 原文

 // 1.创建电脑对象,电脑对象有关机和开机方法
        // 2.创建三个学生对象,并且输出学生的学号、姓名、年级和年龄
        // 3.创建一个水果对象,输出水果的种类,颜色,重量
        function Compter(uName) {
            this.uName = uName;
            this.Colse = function() {
                alert('正在关机!');
            };
            this.Dose = function() {
                alert('开机中!');
            };
        }
        var deer = new Compter('戴尔');
        console.log(deer.uName);
        // deer.Colse();
        // deer.Dose();
        function Student(num, uName, grade, age) {
            this.num = num;
            this.uName = uName;
            this.grade = grade;
            this.age = age;
        }
        var xm = new Student(2, '小明', 7, 12);
        var xh = new Student(3, '小红', 7, 11);

        function fun(obj) {
            for (var k in obj) {
                console.log(obj[k]);
            }
        }
        fun(xm);
        fun(xh);

        function Fruit(uName, color, heft) {
            this.uName = uName;
            this.color = color;
            this.heft = heft;
            this.fun = function(fun) {
                console.log(uName + fun);
            }
        }
        var pg = new Fruit('苹果', '红色', 1);
        var ll = new Fruit('榴莲', '黄色', 2);
        fun(pg);
        fun(ll);
        // console.log(pg.fun('很甜'));

 

推荐阅读