zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    转载:怎样用通俗的语言解释REST,以及RESTful?
    WiresShark 图解教程1
    派力奥 1.3 发动机
    EtherChannel Cisco 端口聚合详解
    Linux 排除问题的前5分钟
    Linux 定时任务crontab
    Linux SCP命令复制传输文件的用法
    Linux 挂载aliyun数据盘
    BCDedit 研究
    Bcdedit命令使用详解使用方法
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1560656.html
Copyright © 2011-2022 走看看