zoukankan      html  css  js  c++  java
  • 深入研究下javascript的面向对象特性(一)

    <html>
        <head>
            <script type="text/javascript">
                function addElement(msg) {
                    var _li = document.createElement('li');
                    var _text = document.createTextNode(msg);
                    var resultBoard = document.getElementById('resultBoard');
                    _li.appendChild(_text);
                    resultBoard.appendChild(_li);
                }
                //动物构造函数
                function Animal() {
                    this.constructor = Animal;
                }
                Animal.prototype = new Object();
                //人构造函数
                function Person(name,age,sex){
                    this.name = name;
                    this.age = age;
                    this.sex = sex;
                    this.say = function () {                    
                        alert('I am a Person,My name is '+ this.name + ' and ' + 'my age is '+ this.age + ' and I am a ' + this.sex + '!');
                    }
                    this.constructor = Person;
                }
                Person.prototype = new Animal();
                
                function Chinese(name,age,sex,party) {
                    this.home = 'chinese';
                    this.party = party;
                    Person.call(this,name,age,sex);
                    this.constructor = Chinese;
                }
                
                Chinese.prototype = new Person();
                
            </script>
            <script type="text/javascript">
                window.onload = function () {
                    var obj1 = new Object();
                    addElement('obj1的构造函数是:'+obj1.constructor);
                    var animal1 = new Animal();
                    addElement('animal1的构造函数是:'+animal1.constructor);
                    var jack = new Person('jack',21,'boy');
                    addElement('jack的构造函数是:'+jack.constructor);
                    jack.say();
                    var fangming = new Chinese('fangming',28,'boy','none');
                    addElement('fangming的构造函数是:'+fangming.constructor);
                    fangming.say();
                    var denglili = new Chinese('denglili',27,'girl','none');
                    addElement('denglili的构造函数是:'+denglili.constructor);
                    denglili.say();
                }
            </script>
            
        </head>
        <body>
            <h3>javascript Object 测试</h3>
            <hr/>
            <ul id="resultBoard">
                <li>输出结果</li>
            </ul>
        </body>
    </html>
  • 相关阅读:
    基于KNN的newsgroup 18828文本分类器的Python实现
    基于Bayes和KNN的newsgroup 18828文本分类器的Python实现
    C++笔试题
    一号店笔试题
    最长递增子序列
    雅虎2015校招--研究工程师
    百度2013校园招聘笔试题(答案整理) – 机器学习/数据挖掘工程师
    windows下Python shell代码自动补全
    windows下scrapy安装
    实验四
  • 原文地址:https://www.cnblogs.com/treemanfm/p/2587263.html
Copyright © 2011-2022 走看看