• JS面向对象编程(对象创建)


    JS面向对象编程(对象创建)

    //构造函数
    function Animal(name, sound, age) {
        this.name = name;
        this.sound = sound;
        this.age = age;
    }

    //类属性
    Animal.description = 'animal';

    //类方法
    Animal.descript = function() {
        document.write('This is a \'' + this.description + '\' class!<br>');
    }

    //实例方法
    Animal.prototype = {
        sayName:function(str) {
                    document.write('My name is:' + this.name + str + '<br>');
                },
        shout:function() {
                  document.write('My sound is:' + this.sound + '!!!<br>');
              },
        sayInfo:function() {
                    document.write('Name = ' + this.name + '; Age = ' + this.age + '<br>');
                }
    };

    //测试类
    Animal.descript();
    var dog = new Animal('dog', 'wang wang', 5);
    var cat = new Animal('cat', 'miao miao', 3);
    dog.sayName('.--wang');
    cat.sayName('.--miao');
    dog.shout();
    cat.shout();
    dog.sayInfo();
    cat.sayInfo();

    //输出
    /**
     * This is a 'animal' class!
     * My name is:dog.--wang
     * My name is:cat.--miao
     * My sound is:wang wang!!!
     * My sound is:miao miao!!!
     * Name = dog; Age = 5
     * Name = cat; Age = 3
     **/

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sgear/archive/2008/07/27/2720194.aspx

  • 相关阅读:
    截取表单提交的字符串信息转换成对象 -- 前端面试题(一)
    HTML5 简单归纳 -- 前端知识 (二)
    02_安装Linux
    01_Linux 简介
    Mysql学习笔记八:Mysql操作
    Mysql学习笔记七:常用SQL语句
    Mysql学习笔记六:事务
    Mysql学习笔记五:修改
    Mysql学习笔记四:查询
    Mysql学习笔记二:主键、外键
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1560656.html
Copyright © 2020-2023  润新知