• 原型1


    1、hasOwnProperty():判断属性是不是自身属性,是返回true=>对象.hasOwnProperty(属性)

         in:自身和原型都返回true =>属性 in 对象

    function Animate(name){
      this.name = name;   //自身属性
    }
    Animate.prototype.age
    = 20; //原型属性 var c = new Animate('aaa'); console.log(c.hasOwnProperty('name')); //true console.log(c.hasOwnProperty('age')); //false console.log('name' in c ) // true console.log('age' in c ) //true for (var attr in c){ if (c.hasOwnProperty(attr)){ console.log(attr); //只会打印name } }

    因此通过以上属性,就可以判断是存在对象中?还是原型中?

            function hasPrototypeProperty(object,name){
                return !object.hasOwnProperty(name) && (name in object);    // 原型返回true,自身属性返回false
            }

    一些对比,如下:

    2、propertyIsEnumerable():用来判断是否可枚举,返回boolean
      *自身属性可枚举
      *原型属性、内建属性和方法不可枚举

    console.log( c.propertyIsEnumerable('name') );  //name由于是自身属性,因为可枚举,true
    console.log( c.propertyIsEnumerable('age') ); //age 是在原型上,是不可枚举的,false
    
    console.log( c.constructor.prototype.propertyIsEnumerable('age') ) ; //  c.constructor指向的是构造函数Animate,当Animate.prototype.prototypeIsEnumerable这时原型就是可枚举的,true


    3、确定原型和实例的关系:instanceof 或 isPrototypeOf ,可以判断引用类型

      new出来的对象 instanceof 构造函数

      构造函数.prototype.isPrototypeof( new出来的对象)

    console.log(c instanceof Animate)   // true
    console.log(c instanceof Object )  //true
    
    console.log( Object.prototype.isPrototypeof(c) ) //true
    console.log( Animate.prototype.isPrototypeof(c) ) //true

       toString用来做引用类型的判断

      var arr = [];
      console.log(Object.prototype.toString.call(arr));//[object Array]
    
      console.log(Object.prototype.toString.call(arr) == '[object Array]'); // true
    
       /*
        []           : '[object Array]'
        {}           : '[object object'
        new Date     :'[object Date]' 
        null         :'[object Null]' 
        new RegExp   : [object RegExp] 
       */

    使用toString是相对完美的解决方法,用instanceof或者constructor,在iframe中无法判断引用类型,例子如下:

        window.onload = function(){
          var oF = document.createElement('iframe');
          document.body.appendChild(oF);
          var ifArray = window.frames[0].Array;  //iframe下的array
          var arr = new ifArray();  //iframe下创建一个数组
          console.log(arr.constructor == Array); //false
          console.log(arr instanceof Array); //false
          console.log(Object.prototype.toString.call(arr) == '[object Array]')  //true
        }

    4、Object.getPrototypeOf:找到原型,返回Object

    var lang = {
      'cn':'简体',
      'tw':'繁体'
    };
    
    function He(){}
    He.prototype = lang;
    
    var k = new He();
    
    console.log(lang.isPrototypeOf(k));   // true
    
    console.log(Object.getPrototypeOf(k));  //k上的原型是lang对象

     

    其他:

    1、原型链__proto__这个,最终都会找到最上层的Object对象
    2、当He.prototype={},这时会更改到constructor,需要重新指定一下,即:He.prototype = {constructor:He}
    3、可以扩展内建对象(Array,String等),如String.prototype.reverse= funciton(){},为防止将来被扩展,需要做一下检测工作,即:if (typeof String.prototype.reverse != 'function')

  • 相关阅读:
    Python学习 5day__基础知识
    pycharm 2018.1 专业版激活 亲测可用!!!
    JQuery 中 find() 和 filter() 的区别
    React 事件处理
    React 数据传递
    js操作cookie的一些注意项
    html5的技术要点
    css背景设置,让套图中某张图片居中显示的例子
    js对象封装内部图片的相关代码,采用base64图片串
    针对网上很多抱怨的言论,写了一个EF中update对象时,通用的遍历赋值方法,以供参考
  • 原文地址:https://www.cnblogs.com/joya0411/p/5345348.html
Copyright © 2020-2023  润新知