zoukankan      html  css  js  c++  java
  • 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)   
         //不是自身属性,但能够访问到 就是原型属性   
    } 
  • 相关阅读:
    缓存更新的套路
    return 和 return false 的区别
    TensorFlow函数:tf.truncated_normal
    TensorFlow随机值:tf.random_normal函数
    TensorFlow函数:tf.lin_space
    TensorFlow函数:tf.ones_like
    TensorFlow函数:tf.ones
    TensorFlow函数:tf.zeros_like
    Tensorflow函数:tf.zeros
    数据分析常用的python工具和SQL语句
  • 原文地址:https://www.cnblogs.com/yangjingqi/p/4337514.html
Copyright © 2011-2022 走看看