JavaScript--------instanceof

本文深入探讨了JavaScript中对象原型的使用方式,并通过实例展示了如何为内置类型如String添加自定义方法。此外,还介绍了如何利用Function.prototype进行方法扩展。

//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>

转载于:https://www.cnblogs.com/syf/archive/2012/09/30/2709334.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值