js 继承(2 使用prototype.js)

本文介绍了一种在JavaScript中扩展内置对象的方法,通过Object.extend方法可以方便地为String原型添加多种实用功能,如去除HTML标签、转换为数组及驼峰命名等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//destination 子对象

//source 父对象

Object.extend = function(destination,source){

    for(property in source){

        destination[property] = source[property];

    }

    return destination;

}

Object.extend(String.prototype,{

    stripTags:function(){

        return this.replace(/<//?[^>]+>/gi,'');

    },

    toArray:function(){

        return this.split('');

    },

    comelize:function(){

        var oStringList = this.split('-');

        //处理-

        if(oStringList.length == 1){

            return oStringList[0];

        }

 

        //主要处理background-这样的字符串的

        var camelilzedString = this.indexOf('-') == 0?

        oStringList[0].charAt(0).toUpperCase()+oStringList[0].substring(1):

        oStringList[0];

        for(var i=1; i<oStringList.length; i++){

            var s = oStringList[i];

            camelilzedString += s.charAt(0).toUpperCase()+s.substring(1);

        }

        return camelilzedString;

    }

});

alert("<b><i>Hello</i>,world!".stripTags());

alert("abcdefg".toArray()[3]=='d');

alert("background-color".comelize());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值