js中关于原型的几个方法

一、isPrototypeOf()方法,判断一个对象是否是另一个对象的原型

 function Student(name,age){
             this.name=name;
            this.age=age;
            
         }
var student = new Student('yjj',15);

alert(Student.prototype.isPrototypeOf(student));//true

二、Obeject.getPrototypeOf()方法,获取某个对象的原型对象

function Student(name,age){
             this.name=name;
            this.age=age;
            
         }
var student = new Student('yjj',15);
alert(Object.getPrototypeOf(student )==Student.prototype);//true

此方法为ECMAScript 5新增,支持该方法的浏览器有IE9+,Firefox 3.5+,Safari 5+,Opera 12+,Chrome.

三、hasOwnProperty()方法判断对象是不是自身的属性

function Student(name,age){
             this.name=name;
            this.age=age;
            
         }
Student.prototype.xxx='ssss';
var student = new Student('yjj',15);
alert(student.hasOwnProperty('name'));//true
alert(student.hasOwnProperty('xxx'));//false

四、in操作符,in操作符单独使用时,会在通过对象能够访问给定属性时返回true,无论该属性存在于实例还是原型中。

function Student(name,age){
             this.name=name;
            this.age=age;
            
         }
Student.prototype.xxx='ssss';

var student = new Student('yjj',15);
alert("name" in student); //true
alert("xxx" in student); //true

 判断一个对象的属性是否是原型属性。

function hasPrototypeProperty(object,name){
     return !object.hasOwnProperty(name)&&(name in object)   
     //不是自身属性,但能够访问到 就是原型属性   
} 
原文地址:https://www.cnblogs.com/yangjingqi/p/4337514.html