[转] [Flash/Flex] 为字符串原型添加splice()方法

[url]http://bbs.9ria.com/thread-79571-1-1.html[/url]

资讯类型: 翻译
来源页面: http://www.bennadel.com/blog/2160-Adding-A-Splice-Method-To-The-Javascript-String-Prototype.htm
资讯原标题: Adding A Splice() Method To The Javascript String Prototype
资讯原作者: Ben Nadel
翻译词数: 词
我的评论:挺好的
对这篇文你有啥看法,跟贴说说吧!欢迎口水和板砖,哈哈。欢迎大家和我们一同分享更多资讯。
在我昨天发的博客上,关于在javascript中使用slice(),substring(),和substr()进行子串提取这一问题,我和Andy Matthews简短的讨论了,无论在哪种语言的核心层面,字符串就是一个字符数组。这个概念引发了我如下思考——如果数组和字符串在Javascript都有slice()方法,那么为什么他们不都有一个splice()方法呢?呵呵,我真的想目睹一下,当增加了我们自己的splice()方法后,修改字符串原型到底能变得多简单,当然,这只是开个玩笑。
Javascript已经在数组中支持splice()。所以,不用为字符串重新写Splice()方法,我想最好的做法就是,把我们的目标字符串先转为数组,然后再间接的利用数组中已经定义好的splice()。幸运的是,使用split()和join()两个方法,可以很容易的将字符串转换为数组:
String.split(""),将字符串转换为字符数组
Array.join("") 将字符数组转换为字符串
好吧,让我们来看看一些代码。在接下来的Demo中,我将更新核心字符串的原型。那意味着从今往后我们splice()方法将适用于所有字符串的情况(因为他们都继承自String对象原型)
<!DOCTYPE html>

<html>
<head>
<title>Creating A String.splice() Method In Javascript</title>

<script type="text/javascript">


// Extend the String prototype to include a splice method.
// This will use an Array-based splitting / joining approach
// internally.
String.prototype.splice = function(
index,
howManyToDelete,
stringToInsert /* [, ... N-1, N] */
){

// Create a character array out of the current string
// by splitting it. In the context of this prototype
// method, THIS refers to the current string value
// being spliced.
var characterArray = this.split( "" );

// Now, let's splice the given strings (stringToInsert)
// into this character array. It won't matter that we
// are mix-n-matching character data and string data as
// it will utlimately be joined back into one value.
//
// NOTE: Because splice() mutates the actual array (and
// returns the removed values), we need to apply it to
// an existing array to which we have an existing
// reference.
Array.prototype.splice.apply(
characterArray,
arguments
);

// To return the new string, join the character array
// back into a single string value.
return(
characterArray.join( "" )
);

};


// -------------------------------------------------- //
// -------------------------------------------------- //
// -------------------------------------------------- //
// -------------------------------------------------- //


// Create our test string value.
var message = "Katie is sort of cool.";

// Set the part of the string that we want to delete.
var lameStuff = "sort of cool";

// Now, let's create a more endearing message.
var betterMessage = message.splice(
message.indexOf( lameStuff ),
lameStuff.length,
"crazy-insane kinds of hot"
);

// Output the new message.
console.log(
"New message:",
betterMessage
);


</script>
</head>
<body>
<!-- Intentionally left blank. -->
</body>
</html>


正如你所看到的,整个流程实际上是很简单:
1.将字符串转换成数字符数组。
2.对字符数组使用splice()方法
3.将字符数组转换为字符串
当我们运行上述代码,我们得到以下输出结果:
New message: Katie is crazy-insane kinds of hot.

正如你看到的,现有的子串"sort of cool"被删除,“crazy-insanekinds of hot”被插入。当这个操作被执行,我们的基本数据结构成为一个字符数组值。然而,我们插入的是一个字符串,这种混合的“数据类型”无所谓,因为当我们使用join()方法时,所有的这些将变成一个字符串值。
注意:我用“数据类型”在这里是用的losest的意思。Javascript并不能真正区分字符串和字符(一个字符是一个sting长度)。
虽然这是一种随意而为,我想不出一个伟大的使用案例使用了splice()方法。典型的情况是,当我用一个字符串(可以为空)来代替另一个字符串,它是通过使用Javascript的规范表达替换方法。但即使如此,这个探索肯定会帮助人们发现,使用原型继承机制是很不错的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值