对象原型prototype介绍

<script type="text/javascript">
/*
prototype原型对象

prototype可以理解成对象的特殊属性,实例后,这个属性不会被实例,而这个属性下的所有成员则会被实例并且被追加到对象下。。。即
*/

var o = new Function;
o.prototype = {

    a : function () {
        alert("a");
    }   
    , b : function () {
        alert("b");
    }
   
}

var c = new o;

c.a(); //a
c.b(); //b

/*
prototype.xx和this.xx最大的区别,即是引用的指向问题。。。
如下code
*/

var class1 = function () {
    this.a = function () {
        alert("a");
    };
};

var class2 = function () {};

class2.prototype.a = function () {
    alert("a");
};

var c1_1 = new class1;
var c1_2 = new class1;
var c2_1 = new class2;
var c2_2 = new class2;

alert(c1_1.a === c1_2.a); // false
alert(c2_1.a === c2_2.a && c2_2.a === class2.prototype.a); //true

/*
向这样实例4个对象以后
c1_1和c1_2的a方法,分别分配了不同的func

c2_1和c2_2的a方法,只分配了class2.prototype.a那个func
这样,如果这个类有若干若干多的实例的话,很显然class2会比class1节省若干资源
*/
</script> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值