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

  • 相关阅读:
    TCP重传
    GIT常用命令参考图
    nodepad++ 快捷键加常用操作
    Django学习之manage.py使用
    Python遍历目录
    Pycharm 2017 12月最新激活码
    Python 基础之列表去重的几种玩法
    ddos cc攻击简单介绍(转)
    linux升级python版本至3
    linux解压xxx.tar.xz文件
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1560656.html
Copyright © 2011-2022 走看看