悟生慧

 

JavaScript的prototype的属性和方法-使用

function Person(name, sex) {
    this.name = name;
    this.sex = sex;
}
Person.prototype = {
    getName: function () {
        return this.name;
    },
    getSex: function () {
        return this.sex;
    }
}
Person.prototype.age = 25;
var zhang = new Person("ZhangSan", "man");
var chun = new Person("ChunHua", "woman");

function Employee(name, sex, employeeID) {
    this.name = name;
    this.sex = sex;
    this.employeeID = employeeID;
}

function ShowCase() {
        alert(zhang.getName());
        alert(chun.getName());
        alert(zhang.age);
        alert(chun.age);

        
        Employee.prototype = new Person('vf','sf');
        Employee.prototype.getEmployeeID = function () {
            return this.employeeID;
        };
        var wa = new Employee("wawu", "man", "1234");
        alert(wa.getName());
        alert(wa.getEmployeeID());
        
    }

 

posted on 2012-08-28 10:53  悟生慧  阅读(158)  评论(0编辑  收藏  举报

导航