首页 > 代码库 > 原型的面向对象

原型的面向对象

<script type="text/javascript">
    function Person(name, age) { //构造函数
        this.name = name; //只写属性
        this.age = age;
    }
    Person.prototype.showName = function() { // 用的共同的父亲
        alert("my name is " + this.name);
    }
    Person.prototype.showAge = function() {
        alert("my age is " + this.age);
    }
    var demo1 = new Person("李四", 15);
    var demo2 = new Person("李四", 15);
    var demo3 = new Person("张三", 15);
    demo1.showName();
    alert(demo1.showName == demo2.showName); //true
    alert(demo1.showName == demo3.showName); //true
</script>

原型的面向对象