我们创建构造函数的时候,原型对象上会有一个constructor属性,它是原型对象所独有的
1.instanceof
2.constructor

  1. function Person(name,age){
  2. this.name = name;
  3. this.age = age;
  4. }
  5. var p = new Person("chen",20);
  6. console.log(p.constructor==Person);
  7. var arr = [1,2,3];
  8. console.log(arr.constructor==Array);
  1. function Person(name,age){
  2. this.name = name;
  3. this.age = age;
  4. }
  5. Person.prototype.sayName = function(){
  6. console.log(this.name);
  7. }
  8. var p = new Person("chen",20);
  9. console.log(p.constructor==Person);

改变原型对象

以字面量(对象)的形式给原型对象添加属性
就是我们以直接量(对象)形式,给原型对象添加属性的时候,
它的constructor会指向Objecct

  1. function Person(name,age){
  2. this.name = name;
  3. this.age = age;
  4. }
  5. // 以字面量(对象)的形式给原型对象添加属性
  6. Person.prototype = {
  7. sayName:function(){
  8. console.log(this.name);
  9. },
  10. sayAge(){
  11. console.log(this.age);
  12. }
  13. }
  14. /*
  15. 问题:就是我们以直接量(对象)形式,给原型对象添加属性的时候,
  16. 它的constructor会指向Objecct
  17. */
  18. var p = new Person("chen",20);
  19. console.log(p.constructor==Person);
  20. </script>
          function Person(name,age){
            this.name = name;
            this.age = age;
        }
        /*
        问题:就是我们以直接量(对象)形式,给原型对象添加属性的时候,
        它的constructor会指向Objecct
        需要:重置constructor
        */
        Person.prototype = {
            constructor:Person,
            sayName:function(){
                console.log(this.name);
            },
            sayAge(){
                console.log(this.age);
            }
        }

        var p = new Person("chen",20);
        console.log(p.constructor==Person);