日期:2014-05-16  浏览次数:20396 次

Javascript 中的继承
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Javascript 中的继承</title>
    <script type="text/javascript">
        function Person(name) {
            this.name = name;
        }
        
        Person.prototype.getName = function() {
            return this.name;
        }
        
        function Man(name, sex) {
            Person.call(this, name);
            this.sex = sex;
        }
        
        Man.prototype = new Person();
        
        Man.prototype.getSex = function() {
            return this.sex;
        }
        
        var person = new Person('stefan');
        var mPerson = new Man('stefan2', 'man');
        
        alert(person.getName());
        
        alert(mPerson.getName());
        alert(mPerson.getSex());
    </script>
</head>
<body>

</body>
</html>