JavaScriptの原型の書き換え

850 ワード

<script>    
/**
     *           
     *       ,      Person.prototype   
     *    constructor     Person    Object
     *   constructor      ,   json        
     */
    function Person() {};
    Person.prototype = {
        name:"Jack",
        age:25,
        say:function() {
            alert(this.name + ", " + this.age);
        }
    };
    var p1 = new Person();
    p1.say();
    alert(p1.constructor);
    alert(p1.constructor == Person);

    function Student() {};
    Student.prototype = {
        constructor:Student, //     constructor
        name:"Ann",
        age:21,
        say:function() {
            alert(this.name + ", " + this.age);
        }
    };
    var s1 = new Student();
    s1.say();
    alert(s1.constructor == Student);

</script>