zoukankan      html  css  js  c++  java
  • JavaScriptinstanceof

    //alert(test.prototype);

    //alert(Object.prototype);

    //alert(Function.prototype);

    //alert(test.__proto__);

    //alert(Object.__proto__);

    //alert(Function.__proto__);

    //alert(Object.prototype.__proto__);

    //alert(Object.prototype.constructor);

    alert(Function.prototype.__proto__);

    alert(Function.prototype.__proto__.__proto__);

    alert(Function.__proto__===Object.__proto__);

    alert(Function instanceof Function);//true 

    alert(Function instanceof Object);//true    

    alert(Object instanceof Function);//true 

              

    function Foo() {};

    var foo = new Foo();

    alert(foo instanceof Foo); // true

    alert(foo instanceof Function); // false

    alert(foo instanceof Object); // true

    alert(Foo instanceof Function); // true

    alert(Foo instanceof Object); // true

     

     


    instanceof 检测一个对象A是不是另一个对象B的实例的原理是:查看对象Bprototype指向的对象是否在对象A[[prototype]]链上。如果在,则返回true,如果不在则返回false。不过有一个特殊的情况,当对象Bprototypenull将会报错(类似于空指针异常)

    <script>

    Function.prototype.method = function(name,func){

    //mark_1

           this.prototype[name] = func;

           return this;

    };

    String.method("trim",function(){                         

           return this.replace(/^\s+|\s+$/g,'');

    });

    String.prototype.test=function()

    {

           return this+"你好";

    }

    alert(" test ".test());

    </script>

  • 相关阅读:
    python3 基本使用多线程
    img前置显示屏装load图片
    leetcode
    亲串 (hdu 2203 KMP)
    基于VC面部识别软件(识别出人脸特征)
    IT该忍者神龟Oracle 树操作(select…start with…connect by…prior)
    It&#39;s about trust
    hdu 2128 Frog(简单DP)
    第四十天 阿乐在其中—Android小游戏的飞机(四)加入敌人
    Mac OS X通过结合80port
  • 原文地址:https://www.cnblogs.com/syf/p/2709334.html
Copyright © 2011-2022 走看看