1. hasOwnProperty()可以判断属性时私有的还是共有的

2.在构造函数中

(1)共有:一般在原型对象上

(2)私有属性,通过this关键字去添加的

  1. <script>
  2. function Person(name,age){
  3. this.name=name;
  4. this.age=age;
  5. }
  6. /*在构造函数中
  7. 共有:一般在原型对象上
  8. 私有属性,通过this关键字去添加的
  9. hasOwnProperty()可以判断属性时私有的还是共有的
  10. */
  11. Person.prototype= {
  12. constructor:Person,
  13. sayName(){
  14. console.log(this.name);
  15. },
  16. sayAge(){
  17. console.log(this.age);
  18. }
  19. }
  20. var p=new Person("zhang",20);
  21. console.log(p.hasOwnProperty("name")); //true
  22. console.log(p.hasOwnProperty("sayName")); //false
  23. </script>